# How can I iterate through two lists in parallel in Python?

In Python, using a for loop is common to iterate a single data structure like list, but what if we need to iterate two/multiple lists parallelly?. In this article we will see different ways to iterate two/multiple lists parallelly.

By using range() function, we can iterate the two lists parallelly. Lets see the below examples and how we are going to iterate two lists in python.

## Using the for loop If length of the two lists are same

Assuming that both lists have the same length, here we are using the len() method to get the length of the list object.

### Example

l1 = ['a', 'b', 'c', 'd', 'e']
l2 = [97, 98, 99, 100, 101]
length = len(l1) # Assuming both lists have same length

for i in range(length):
print(l1[i], l2[i])


### Output

a 97
b 98
c 99
d 100
e 101


## If length of the two list are not same

Assuming that both lists are unequal, we will iterate the for loop using a range of minimum length.

### Example

L1 = [1, 2, 3, 4, 5, 6, 7]
L2 = ['a', 'b', 'c']
length = len(L1) if len(L1)<=len(L2)else len(L2)

for i in range(length):
print(L1[i], L2[i])


### Output

1 a
2 b
3 c


The list objects l1, l2 have lengths 7 and 3 respectively, and we applied the minimum length 3 to the range() function so that we successfully iterated the two lists parallelly. Note: If we iterate the for loop using the maximum length, then it will raise an IndexError.

## Using the zip() function

The zip() function is a python built-in function that returns a zip object which represents an iterator of tuples. The zip() function runs upto the smallest of all the iterables.

### Example

l1 = [1, 2, 3, 4, 5, 6, 7]
l2 = ['a', 'b', 'c']

for ele_l1, ele_l2 in zip(l1, l2):
print(ele_l1, ele_l2)


### Output

1 a
2 b
3 c


Like as previous example, the zip() function also iterated the for loop upto the minimum length among two input iterables.

## Using the itertools.zip_longest() method

The zip_longest() method takes multiple iterables as arguments and returns an iterator. The working of this function is exactly the same as the zip() function, but it works based on the longest input iterable.

### Syntax

zip_longest( iterable1, iterable2, fillval)


### Example

The unmatched elements are filled with None defaultly, and we can specify filled values using the fillvalue parameter.Before using the zip_longest() function we need to import itertools module initially.

import itertools

l1 = [1, 2, 3, 4, 5, 6, 7]
l2 = ['a', 'b', 'c']

for ele_l1, ele_l2 in itertools.zip_longest(l1,l2):
print(ele_l1, ele_l2)


### Output

1 a
2 b
3 c
4 None
5 None
6 None
7 None


### Example

In this example we successfully iterated the 3 lists parallelly.

import itertools

l1 = [1, 2, 3, 4, 5, 6, 7]
l2 = ['a', 'b', 'c', 'd', 'e']
l3 = [10, 11, 12, 13]

for ele_l1, ele_l2, ele_l3 in itertools.zip_longest(l1, l2, l3):
print(ele_l1, ele_l2, ele_l3)


### Output

1 a 10
2 b 11
3 c 12
4 d 13
5 e None
6 None None
7 None None


Updated on: 24-Aug-2023

583 Views