Iterate Through Two Lists in Parallel Python Using itertools

(Last Updated On: 27/03/2023)

Python Program to Iterate Through Two Lists in Parallel Using itertools

# Iterate Through Two Lists in Parallel Using itertools in python

import itertools

list_1 = [1, 2, 5, 4]
list_2 = ['a', 'b', 'c']

# loop until the short loop stops
for i,j in zip(list_1,list_2):
    print(i,j)

print("\n")

# loop until the longer list stops
for i,j in itertools.zip_longest(list_1,list_2):
    print(i,j)

Output:

1 a
2 b
5 c


1 a
2 b
5 c
4 None

Leave a Reply

Your email address will not be published. Required fields are marked *