Open In App

Iterate over Multiple Lists Simultaneously - Python

Last Updated : 21 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Python
import itertools

num = [1, 2, 3]
color = ['red', 'white', 'black']
value = [255, 256]

for a, b, c in zip(num, color, value):
    print(a, b, c)

Output
1 red 255
2 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).

Python
import itertools

num = [1, 2, 3]
col = ['red', 'white', 'black']
val = [255, 256]

for a, b, c in itertools.zip_longest(num, col, val):
    print(a, b, c)

Output
1 red 255
2 white 256
3 black None

Explanation: itertools.zip_longest() ensures that iteration continues even when one list is exhausted, filling missing values with None by default.

3. Using itertools.zip_longest() with fillvalue

You can specify a default value instead of None when using itertools.zip_longest().

Python
import itertools

num = [1, 2, 3]
col = ['red', 'white', 'black']
val = [255, 256]

for a, b, c in itertools.zip_longest(num, col, val, fillvalue=-1):
    print(a, b, c)

Output
1 red 255
2 white 256
3 black -1

Explanation: when one list is shorter, the missing values are replaced with the specified fillvalue (-1 in 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

The enumerate() 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.

Python
l1 = [1, 2, 3]
l2 = ['a', 'b', 'c']
l3 = ['x', 'y', 'z']

for i, ele in enumerate(l1):
    print(ele, l2[i], l3[i])

Output
1 a x
2 b y
3 c z

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.

Python
for item1, item2, item3 in zip((1, 2, 3), ('a', 'b', 'c'), (True, False, True)):
    print(item1, item2, item3)

Output
1 a True
2 b False
3 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:


Next Article
Article Tags :
Practice Tags :

Similar Reads