Python | How to iterate through two lists in parallel?

Python | How to iterate through two lists in parallel?

Iterating on one list is simple and can be done by writing a for loop, but how can we iterate through two lists in parallel? So, In this article, we will cover different ways how to iterate through two lists in parallel.

We will start from the simple approach of using loops and build our way towards slightly advanced approaches like using zip(), map(), and lambda functions. But before proceeding ahead in this article, you should have a basic understanding of these topics-

Method 1: Using a for loop

This is the simplest approach to iterate through two lists in parallel. First, we find the length of both lists, and using the min() function we take the shorter length to make sure that each item from both lists is paired correctly. Then using a for loop we go through each item in the list and every time in the iteration we print the current index element in both lists.

list1 = [1, 2, 3]
list2 = ['python', 'java', 'c++']
for i in range(min(len(list1), len(list2))):
    print(list1[i], list2[i])

Output:

1 python
2 java
3 c++

Method 2: Using the built-in zip() function

We can use the zip() function to zip both the list elements together. The zip() function returns a zip object which is an iterator and it runs till the smaller list is exhausted means it runs only till the length of the smaller list.

list1 = [1, 2, 3]
list2 = ['python', 'java', 'c++', 'javascript']
for item1, item2 in zip(list1, list2):
    print(item1, item2)

Output:

1 python
2 java
3 c++

Method 3: Using the zip_longest() function

In the itertools module, we get the function zip_longest(). As the name is, the function keeps on zipping all the elements of both lists till the longest length list is exhausted. And at the places where the shorter length list elements no longer exist, it places the None value.

from itertools import zip_longest

list1 = [1, 2, 3]
list2 = ['python', 'java', 'c++', 'javascript']

for item1, item2 in zip_longest(list1, list2):
    print(item1, item2)

Output:

1 python
2 java
3 c++
None javascript

We can provide a default value to print instead of None if the elements of the smaller list are fully exhausted. We can do so by providing the argument fillvalue in the zip_longest() function

from itertools import zip_longest

list1 = [1, 2, 3]
list2 = ['python', 'java', 'c++', 'javascript']

for item1, item2 in zip_longest(list1, list2, fillvalue=-1):
    print(item1, item2)

Output:

1 python
2 java
3 c++
-1 javascript

Method 4: Using the built-in enumerate() function to iterate through two lists in parallel

The enumerate() function iterates over a list by keeping track of the current index of each element and using this index we can access the element of the other list as well. In other words, enumerate() helps in assigning a unique number to each item in a list, which can be used to access the item in the other list.

list1 = [1, 2, 3]
list2 = ['python', 'java', 'c++']

for i, element in enumerate(list1):
    print(element, list2[i])

Output:

1 python
2 java
3 c++

Method 5: Using the map() function

In Python, map() is a built-in function that applies a specific function to each item in an iterable like list or tuple. We can use map() with lambda function to define a function that will be applied to each element in both lists. Here, the lambda function takes two inputs, one element from the first list and one from the second, and prints them together.

list1 = [1, 2, 3]
list2 = ['python', 'java', 'c++']

list(map(lambda x, y: print(x, y), list1, list2))

Output:

1 python
2 java
3 c++

Conclusion

In this article, we have learned various ways how to iterate through two lists in parallel. Now just 2 lists, using the methods listed above we can iterate through any number of lists parallelly. I hope you liked this article.

Thank you for visiting our website.


Also read:

Share:

Author: Ayush Purawr