List in Python

why do we need zip() in python?

zip() is used to combine multiple lists, into a set of tuples.
One good example of a way to use it would be combining X coords with Y coords:

x_coords = [13, 45, 7]
y_coords = [19, 6, 31]

coords = zip(x_coords, y_coords)
print(list(coords))

This will print:

[(13, 19), (45, 6), (7, 31)]

You can read the documentation for more information on it:
docs.python.org/3/library/functions.html#zip

3 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.