How To Iterate Over Two (or More) Lists at the Same Time
Use zip() instead of manually iterating over lists in parallel
If you have had multiple data sets that needed to be jointly computed or displayed, then you probably have experienced the pain of iterating over multiple lists in parallel.
Here’s what that looks like traditionally…
The Traditional Method
Taking the length of one list and using a placeholder index variable, often named i, you manually access each position in the lists.a = [1,2,3,4,5]
b = [10,20,30,40,50]for i in range(len(a)):
print(a[i],b[i])"""
1 10
2 20
3 30
4 40
5 50
"""
Notice the [i] in each list. This seems simple enough, not complex.
However, if you have a larger number of lists or more complex data that requires nested loops, then you’re setting yourself up to easily make mistakes.
There has to be an easier way…
The zip() Function
The zip() function eliminates the guesswork and reduces the likelihood of an error. Taking a variable number of iterable arguments, the function will return a zip object that groups each item by its index.a = [1,2,3,4,5]
b = [10,20,30,40,50]
c = zip(a,b)print(c) # <zip object at 0x7f0a064c4eb0>
Notice that the function returns a zip object, meaning you cannot print it right away. Fortunately, we can coerce it into the data type we want.a = [1,2,3,4,5]
b = [10,20,30,40,50]
c = zip(a,b)print(c) # <zip object at 0x7f0a064c4eb0>print(list(c)) # [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50)]
print(dict(c)) # {1: 10, 2: 20, 3: 30, 4: 40, 5: 50}
print(tuple(c)) # ((1, 10), (2, 20), (3, 30), (4, 40), (5, 50))
Be careful when coercing the zip object into a dictionary. Exactly two arguments are necessary, otherwise an error will be thrown.a = [1,2,3,4,5]
b = [10,20,30,40,50]
c = [100,200,300,400,500]
d = zip(a,b,c)print(dict(d))
# ValueError: dictionary update sequence element #0 has length 3; 2 is required
We can use this function inside a for loop as the iterable value. Below is the traditional parallel index technique refactored using zip().a = [1,2,3,4,5]
b = [10,20,30,40,50]
c = [100,200,300,400,500]
for x,y,z in zip(a,b,c):
print(x,y,z)"""
1 10 100
2 20 200
3 30 300
4 40 400
5 50 500
"""
What other uses for the zip() function have you found? Do you have another way of simultaneously iterating over multiple lists? Share your experiences below in the comments!