Home »
Python
Python Iterate Over Multiple Lists Simultaneously
Last Updated : May 03, 2025
In Python, you can iterate over multiple lists at the same time using built-in functions like zip()
and other methods. In this chapter, we will learn how to iterate over multiple lists simultaneously with the help of examples.
Iterate Over Multiple Lists Using zip()
The zip()
function combines elements from multiple iterables into tuples that allows you to loop through them in parallel.
Example
In this example, we are iterating over two lists names
and ages
simultaneously.
# Creating two lists
names = ["Amit", "Bhavna", "Chirag"]
ages = [25, 30, 35]
# Iterating using zip()
for name, age in zip(names, ages):
print(f"{name} is {age} years old")
The output of the above code would be:
Amit is 25 years old
Bhavna is 30 years old
Chirag is 35 years old
Iterate Over Multiple Lists Using zip()
with Different Length Lists
If the lists have different lengths, zip()
stops when the shortest list ends.
Example
In this example, one list has more items than the other. Only the items with matching indexes will be paired.
# Creating two lists of different lengths
names = ["Amit", "Bhavna", "Chirag", "Deepak"]
ages = [25, 30, 35]
# Iterating using zip()
for name, age in zip(names, ages):
print(f"{name} is {age} years old")
The output of the above code would be:
Amit is 25 years old
Bhavna is 30 years old
Chirag is 35 years old
Iterate Over Multiple Lists Using zip_longest()
from itertools
If you want to continue iteration until the longest list ends, use itertools.zip_longest()
. You can also specify a fill value for missing items.
Example
In this example, we use zip_longest()
to handle lists of unequal lengths.
from itertools import zip_longest
# Creating two lists of different lengths
names = ["Amit", "Bhavna", "Chirag", "Deepak"]
ages = [25, 30, 35]
# Using zip_longest with fillvalue
for name, age in zip_longest(names, ages, fillvalue="N/A"):
print(f"{name} is {age} years old")
The output of the above code would be:
Amit is 25 years old
Bhavna is 30 years old
Chirag is 35 years old
Deepak is N/A years old
Iterate Over Multiple Lists Using enumerate()
You can combine enumerate()
with zip()
to access the index along with the values from multiple lists.
Example
In this example, we are accessing the index and values from two lists simultaneously.
# Creating two lists
cities = ["Mumbai", "Delhi", "Bengaluru"]
countries = ["India", "India", "India"]
# Using enumerate with zip
for i, (city, country) in enumerate(zip(cities, countries)):
print(f"{i}: {city}, {country}")
The output of the above code would be:
0: Mumbai, India
1: Delhi, India
2: Bengaluru, India
Iterate Over Multiple Lists Using List Comprehension with zip()
List comprehensions can also be used with zip()
to create new lists by combining multiple lists.
Example
In this example, we are creating a new list of formatted strings from two existing lists.
# Creating two lists
students = ["Ananya", "Bharat", "Charu"]
grades = ["A", "B", "A+"]
# Using list comprehension with zip
report = [f"{student}: {grade}" for student, grade in zip(students, grades)]
# Displaying the result
print(report)
The output of the above code would be:
['Ananya: A', 'Bharat: B', 'Charu: A+']
Exercise
Select the correct option to complete each statement about iterating over multiple lists at the same time in Python.
- The built-in ___ function is commonly used to iterate over multiple lists in parallel.
- When using
zip(list1, list2)
, iteration stops when ___.
- To keep the iteration going for all elements, even if the lists are of different lengths, use
zip_longest
from the ___ module.
Advertisement
Advertisement