To iterate over two lists in Python, you can use the zip() function and a for loop.
lst_1 = [0, 1, 2]
lst_2 = ["a", "b", "c"]
for x, y in zip(lst_1, lst_2):
print(x, y)
#Output:
0 a
1 b
2 c
If you have lists which don’t have the same length, you can use the itertools module zip_longest() function.
import itertools
lst_1 = [0, 1, 2]
lst_2 = ["a", "b", "c", "d"]
for x, y in itertools.zip_longest(lst_1, lst_2):
print(x, y)
#Output:
0 a
1 b
2 c
None d
When working with collections of data, the ability to iterate over those collections in an easy way is valuable.
One such case is if you have two lists and want to iterate over both lists at the same time in Python.
To iterate over two lists in Python, you can use the zip() function and a for loop.
The Python zip() function zips lists together and returns a zip object, which is an iterator of tuples where each item from each list is paired together.
If the passed iterable objects have different lengths, the iterator with the least items decides the length of the new iterator.
You can then unpack the returned zip object and use the returned values in a for loop.
Below shows how you can iterate over two lists using zip() and a loop in Python.
lst_1 = [0, 1, 2]
lst_2 = ["a", "b", "c"]
for x, y in zip(lst_1, lst_2):
print(x, y)
#Output:
0 a
1 b
2 c
Using itertools zip_longest() Function to Iterate Over Lists of Different Length
If you want to zip two lists which don’t have the same length, you should be aware that the zip() function will remove values from the longer list.
For example, if you have a list of three and five items and try to zip them together, you will get a zip object with three items.
list1 = ["apple", "banana", "cherry"]
list2 = [1, 2, 3, 4, 5]
print(list(zip(list1, list2)))
#Output:
[('apple', 1), ('banana', 2), ('cherry', 3)]
If you want to zip based on the list with the longer length, you can use the itertools module zip_longest() function.
zip_longest() zips two lists and for the items where we cannot pair values, because we don’t have a value from the shorter list, zip_longest() fills in None.
With zip_longest(), you can do the same as above and zip two lists together and iterate over them.
Below shows you how to use zip_longest() to iterate over two lists of different lengths in Python.
import itertools
lst_1 = [0, 1, 2]
lst_2 = ["a", "b", "c", "d"]
for x, y in itertools.zip_longest(lst_1, lst_2):
print(x, y)
#Output:
0 a
1 b
2 c
None d
How to Iterate Over More Than Two Lists in Python
If you have more than two lists, then you can use the same structure as above and just add the additional lists to zip().
You can use the same code structure as above and unpack what is returned from zip() as shown below to iterate over more than two lists in Python.
lst_1 = [0, 1, 2]
lst_2 = ["a", "b", "c"]
lst_3 = [4, 5, 6]
lst_4 = ["d","e","f"]
for x, y, z, a in zip(lst_1, lst_2, lst_3, lst_4):
print(x, y, z, a)
#Output:
0 a 4 d
1 b 5 e
2 c 6 f
Hopefully this article has been useful for you to learn how to iterate over two lists in Python.