How to loop through two lists in Python? [SOLVED]

In this article, we will discuss how to loop through two lists in python. List is an one dimensional data structure which stores elements of multiple data types. In Python we can create list using [] or list() function.

Syntax:

Advertisement
list_data=list()
list_data=[]

 

Different methods to loop through 2 or more lists in Python

Method 1: Using zip()

In this method, we will use zip() function which will map the lists into one and using this, we can iterate/ loop through two lists. Let's see the syntax.

Syntax:

for (iterators...) in zip(lists...):
     print(...)

Here, iterators are used to iterate the elements in a list. Each iterator will iterate all elements in a single list.

So it is important  to check number of iterators will be equal to number of lists. Finally, we can print the iterators using print() statement.

Example:

Let's create two lists such that first list hold strings and second list hold integers and loop through two lists to display them.

Advertisement
# Create two lists with 5 elements each
l1 =['meat', 'fish', 'eggs','veg','fruits']
l2=[100,234,45,67,100]
  
# Iterate above two lists
for (i1,i2) in zip(l1,l2):
     print("Item-: ",i1," Cost-: ",i2)

Output:

Item-:  meat  Cost-:  100
Item-:  fish  Cost-:  234
Item-:  eggs  Cost-:  45
Item-:  veg  Cost-:  67
Item-:  fruits  Cost-:  100

Explanation:

So you can see that i1 and i2 are used as iterators to iterate the lists -  l1 and l2. Finally we are displaying both the iterators.

 

Method 2: Using zip() in List Comprehension

In this method, we will use zip() function which will map the lists into one inside the list comprehension. We are using the list comprehension to iterate the values in the lists.  Let's see the syntax.

Syntax:

[(iterators...) for iterators... in zip(lists...)]

Here, iterators are used to iterate the elements in a list. Each iterator will iterate all elements in a single list.

Advertisement

So it is important  to check number of iterators will be equal to number of lists. We are printing the iterators inside the comprehension - (iterators)

Example:

Let's create two lists such that first list hold strings and second list hold integers and loop through two lists to display them using zip() with list comprehension.

# Create two lists with 5 elements each
l1 =['meat', 'fish', 'eggs','veg','fruits']
l2=[100,234,45,67,100]
  
# Iterate over two lists
print([("Item-: ",i1," Cost-: ",i2) for i1,i2 in zip(l1,l2)])

Output:

[('Item-: ', 'meat', ' Cost-: ', 100), ('Item-: ', 'fish', ' Cost-: ', 234), ('Item-: ', 'eggs', ' Cost-: ', 45), ('Item-: ', 'veg', ' Cost-: ', 67), ('Item-: ', 'fruits', ' Cost-: ', 100)]

Explanation:

So you can see that i1 and i2 are used as iterators to iterate the lists -  l1 and l2. Finally we are displaying both the iterators within the list comprehension.

Advertisement

 

Method 3: Using itertools.zip_longest()

In this method, we will use itertools.zip_longest() function which will map the lists into one and using this, we can iterate/ loop through two lists. It is available in itertools module. It is very important to import this module. Let's see the syntax.

Syntax:

for (iterators...) in itertools.zip_longest(lists...):
     print(...)

Here, iterators are used to iterate the elements in a list. Each iterator will iterate all elements in a single list.

So it is important  to check number of iterators will be equal to number of lists. Finally, we can print the iterators using print() statement.

Example:

Let's create two lists such that first list hold strings and second list hold integers and loop through two lists to display them.

Advertisement
# Import the itertools module
import itertools 

# Create two lists with 5 elements each
l1 =['meat', 'fish', 'eggs','veg','fruits']
l2=[100,234,45,67,100]
  
# Iterate above two lists
for (i1,i2) in itertools.zip_longest(l1,l2):
     print("Item-: ",i1," Cost-: ",i2)

Output:

Item-:  meat  Cost-:  100
Item-:  fish  Cost-:  234
Item-:  eggs  Cost-:  45
Item-:  veg  Cost-:  67
Item-:  fruits  Cost-:  100

Explanation:

So you can see that i1 and i2 are used as iterators to iterate the lists -  l1 and l2. Finally we are displaying both the iterators.

 

Method 4: Using itertools.zip_longest() in List Comprehension

In this method, we will use itertools.zip_longest() function which will map the lists into one inside the list comprehension. We are using the list comprehension to iterate the values in the lists.  Let's see the syntax.

Syntax:

[(iterators...) for iterators... in itertools.zip_longest(lists...)]

Here, iterators are used to iterate the elements in a list. Each iterator will iterate all elements in a single list.

Advertisement

So it is important  to check number of iterators will be equal to number of lists. We are printing the iterators inside the comprehension - (iterators)

Example:

Let's create two lists such that first list hold strings and second list hold integers and loop through two lists to display them using itertools.zip_longest() with list comprehension.

# Create two lists with 5 elements each
l1 =['meat', 'fish', 'eggs','veg','fruits']
l2=[100,234,45,67,100]
  
# Iterate over two lists
print([("Item-: ",i1," Cost-: ",i2) for i1,i2 in itertools.zip_longest(l1,l2)])

Output:

[('Item-: ', 'meat', ' Cost-: ', 100), ('Item-: ', 'fish', ' Cost-: ', 234), ('Item-: ', 'eggs', ' Cost-: ', 45), ('Item-: ', 'veg', ' Cost-: ', 67), ('Item-: ', 'fruits', ' Cost-: ', 100)]

Explanation:

So you can see that i1 and i2 are used as iterators to iterate the lists -  l1 and l2. Finally we are displaying both the iterators within the list comprehension.

Advertisement

 

Summary

In this guide, we have seen how to loop through two lists using 4 different methods. Make sure that you need to specify number of iterators that are equal to number of lists.

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X