How to iterate through two lists in parallel in Python

Before learning how to iterate through 2 lists simultaneously let’s see what is a list in Python.

Lists in Python:

A list in Python is a collection of elements. Here elements are not limited to any data type (the list can contain elements of any type), but all must be of the same datatype. The elements in a list are ordered, but this order can be changed. The elements in a list can be duplicated.

In Python, lists are written with square brackets.

Example:

list = ["India", "Japan", "Canada"]
print(list)

Output:

["India", "Japan", "Canada"]

Access Items

You access the elements of the list by referring to their index number.

Example:

list = ["India", "Japan", "Canada"]
print(list[0])

Output:

India

Now that we have an idea on how to create a list and how to access the elements in the list lets see how to iterate through a single list.

Iterating through a single list:

We can use a for loop to iterating through a loop.

Example:

list = ["India", "Japan", "Canada"] 
for element in list:
    print(element)

Output:

India
Japan
Canada

Commands to Understand:

  1. ‘element’ is called as the iterating variable.
  1. ‘in’ is used to check if the element is present in the list or not.

Now that we know to iterate through a single let us see how to iterate through two or multiple lists in Python.

Iterating through multiple lists at the same time:

The image shown below shows how to iterate through a single or multiple lists.

iteration

To iterate through 2 or more different listsĀ  can be done using 2 functions, they are

  1. zip
  1. itertools.zip_longest

Zip():

To use zip function we must import the itertools module. Importing this module is as same as any other module in python.

Syntax: import itertools

Zip() function must be used when the user wants to stop printing after anyone of the lists completes iterating. In simple words, it runs till the smallest of all the lists.

Itertools.zip_longest():

 

Zip_longest is also a function that is a part of the itertools module. It stops when all lists are exhausted. When the shorter iterator(s) are exhausted, zip_longest prints a tuple with None value.

This method also provides us with the option of adding a default value for the elements in a list that is shorter than the other.

 

Example:

import itertools 
num = [1, 2, 3,4] 
Names = ['Bradley', 'Irving', 'smith','john'] 
marks = [98, 78, 50] 
print('With Zip\n')
for (a, b, c) in zip(num, Names, marks): 
    print (a, b, c)
print('\nWith itertools.zip_longest\n')
for (a, b, c) in itertools.zip_longest(num,Names,marks):
    print (a, b, c)
print('\nWith itertools.zip_longest and default value\n')
for (a, b, c) in itertools.zip_longest(num, Names, marks, fillvalue='Abs'): 
    print (a, b, c)

 

Output:

With Zip

1 Bradley 98
2 Irving 78
3 smith 50

With itertools.zip_longest

1 Bradley 98
2 Irving 78
3 smith 50
4 john None

With itertools.zip_longest and a default value

1 Bradley 98
2 Irving 78
3 smith 50
4 john Abs

 

Leave a Reply

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