Iterating over multiple lists simultaneously allows you to process elements from different lists at the same time. This can be especially useful when dealing with related data stored in multiple lists.

Python provides several methods to achieve this, making it easy to work with multiple iterables in a synchronized manner.
Ways to Iterate Over Multiple Lists Simultaneously
You can use the following methods to iterate over multiple lists in Python:
- Using zip()
- Using itertools.zip_longest()
- Using enumerate()
Let's explore each method in detail with examples.
1. Using zip()
zip()function allows you to iterate over multiple lists at once. It pairs elements from each iterable together, stopping when the shortest list is exhausted.
Pythonimportitertoolsnum=[1,2,3]color=['red','white','black']value=[255,256]fora,b,cinzip(num,color,value):print(a,b,c)
Output1 red 2552 white 256
Explanation:
- zip():This function pairs the elements of the lists. It iterates until the shortest list ends.
- The output shows each tuple of paired elements from the three lists
2. Using itertools.zip_longest()
Unlike zip(),itertools.zip_longest() continues iteration until all lists are exhausted. If one list is shorter, it fills the missing values with None (or a specified fillvalue).
Pythonimportitertoolsnum=[1,2,3]col=['red','white','black']val=[255,256]fora,b,cinitertools.zip_longest(num,col,val):print(a,b,c)
Output1 red 2552 white 2563 black None
Explanation: itertools.zip_longest() ensures that iteration continues even when one list is exhausted, filling missing values withNoneby default.
3. Using itertools.zip_longest() with fillvalue
You can specify a default value instead of None when usingitertools.zip_longest().
Pythonimportitertoolsnum=[1,2,3]col=['red','white','black']val=[255,256]fora,b,cinitertools.zip_longest(num,col,val,fillvalue=-1):print(a,b,c)
Output1 red 2552 white 2563 black -1
Explanation: when one list is shorter, the missing values are replaced with the specified fillvalue (-1in this case).
Note :Python 2.x had two extra functions izip() and izip_longest(). In Python 2.x, zip() and zip_longest() used to return list, and izip() and izip_longest() used to return iterator. In Python 3.x, there izip() and izip_longest() are not there as zip() and zip_longest() return iterator.
4. Using enumerate() to Iterate with Index
Theenumerate() function helps when you want to iterate over a list and keep track of the index of each element. You can use this index to access corresponding elements from other lists.
Pythonl1=[1,2,3]l2=['a','b','c']l3=['x','y','z']fori,eleinenumerate(l1):print(ele,l2[i],l3[i])
Explanation:enumerate() function adds a counter to each element of the iterable. The index returned by enumerate() is then used to access corresponding elements in the other lists.
5. Using Generator Expressions with zip()
You can also use generator expressions with zip() to iterate over multiple lists simultaneously in a more compact form.
Pythonforitem1,item2,item3inzip((1,2,3),('a','b','c'),(True,False,True)):print(item1,item2,item3)
Output1 a True2 b False3 c True
Explanation:Generator expressions allow you to iterate over data without storing all elements in memory at once. This is useful for large datasets or when performance is critical.
Related Articles: