Program to Iterate Through Two Lists in Parallel

Python Program to Iterate Through Two Lists in Parallel

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

List:

A list in Python is a collection of zero or more elements. A list element can be any type of data. It can be a string, a number, or a mixture of the two. In Python, a list is equivalent to an array in C or Java. Lists are mutable, which means you may change their content, and they contain a variety of helpful specialized methods.

When working with Python lists, we may encounter an issue in which we must iterate over two list entries. Iterating one after the other is an option, but it is more time consuming, and a one-two liner is always preferred. Let’s go over several different approaches to doing this assignment.

Given two lists, the task is to iterate two lists simultaneously.

Examples:

Printing list one after other:

Example1:

Input:

samplelistone = ["hello", "this", "is", "BTechGeeks", "Python"]
samplelisttwo = [38, 23, 10, 20]

Output:

printing the samplelistone  hello this is BTechGeeks Python
printing the samplelisttwo  38 23 10 20
printing the two lists elements simultaneously 
hello this is BTechGeeks Python 38 23 10 20

Example2:

Input:

samplelistone = [27823, 9792, "hello", "this", True, 7.6]
samplelisttwo = ['sky', 'is', 'blue', 122, 5.3214, False, 'hello']

Output:

printing the samplelistone  27823 9792 hello this True 7.6
printing the samplelisttwo  sky is blue 122 5.3214 False hello
printing the two lists elements simultaneously 
27823 9792 hello this True 7.6 sky is blue 122 5.3214 False hello

Printing list in parallel:

Example1:

Input:

samplelistone = ["hello", "this", "is", "BTechGeeks", "Python"]
samplelisttwo = [38, 23, 10, 20, 31]

Output:

printing the samplelistone  hello this is BTechGeeks Python
printing the samplelisttwo  38 23 10 20 31
printing the two lists elements simultaneously 
hello 38
this 23
is 10
BTechGeeks 20
Python 31

Example2:

Input:

samplelistone = [27823, 9792, "hello", "this", True, 7.6]
samplelisttwo = ['sky', 'is', 'blue', 122, 5.3214, False, 'hello']

Output:

printing the samplelistone  27823 9792 hello this True 7.6
printing the samplelisttwo  sky is blue 122 5.3214 False hello
printing the two lists elements simultaneously 
27823 sky
9792 is
hello blue
this 122
True 5.3214
7.6 False

Program to Iterate Through Two Lists in Parallel in Python

Below are the ways to iterate through two lists in parallel in python:

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1:Using for loop and “+” operator

The combination of the above functionalities may make our job easier. The disadvantage here is that we may have to concatenate the list, which would require more RAM than desired.

Below is the implementation:

# initializing lists
samplelistone = ["hello", "this", "is", "BTechGeeks", "Python"]
samplelisttwo = [38, 23, 10, 20]
# printing the two original lists
print("printing the samplelistone ", *samplelistone)
print("printing the samplelisttwo ", *samplelisttwo)
# traversing the two lists simulatenously
print("printing the two lists elements simultaneously ")
for value in samplelistone + samplelisttwo:
    print(value, end=" ")

Output:

printing the samplelistone  hello this is BTechGeeks Python
printing the samplelisttwo  38 23 10 20
printing the two lists elements simultaneously 
hello this is BTechGeeks Python 38 23 10 20

Method #2: Using chain() function

This approach is identical to the one before, but it is somewhat more memory efficient because chain() is used to execute the work and internally builds an iterator.

Chain function will be available in itertools.

Below is the implementation:

from itertools import chain
# initializing lists
samplelistone = [27823, 9792, "hello", "this", True, 7.6]
samplelisttwo = ['sky', 'is', 'blue', 122, 5.3214, False, 'hello']
# printing the two original lists
print("printing the samplelistone ", *samplelistone)
print("printing the samplelisttwo ", *samplelisttwo)
# traversing the two lists simulatenously
print("printing the two lists elements simultaneously ")
for value in chain(samplelistone, samplelisttwo):
    print(value, end=" ")

Output:

printing the samplelistone  27823 9792 hello this True 7.6
printing the samplelisttwo  sky is blue 122 5.3214 False hello
printing the two lists elements simultaneously 
27823 9792 hello this True 7.6 sky is blue 122 5.3214 False hello

Method #3:Using zip()

Zip in Python 3 returns an iterator. When any of the lists in the zip() function is exhausted, the function terminates. In other words, it continues until the smallest of all the lists is reached.
The zip method and itertools.izip are implemented below, and itertools.izip iterates over two lists:

Below is the implementation:

import itertools
# initializing lists
samplelistone = [27823, 9792, "hello", "this", True, 7.6]
samplelisttwo = ['sky', 'is', 'blue', 122, 5.3214, False, 'hello']
# printing the two original lists
print("printing the samplelistone ", *samplelistone)
print("printing the samplelisttwo ", *samplelisttwo)
# traversing the two lists simulatenously
print("printing the two lists elements simultaneously ")
for i, j in zip(samplelistone, samplelisttwo):
    print(i, j)

Output:

printing the samplelistone  27823 9792 hello this True 7.6
printing the samplelisttwo  sky is blue 122 5.3214 False hello
printing the two lists elements simultaneously 
27823 sky
9792 is
hello blue
this 122
True 5.3214
7.6 False

Method #4:Using itertools.zip_longest()

When all lists have been exhausted, zip longest comes to a halt. When the shorter iterator(s) have been exhausted, zip longest returns a tuple with the value None.
The itertools implementation is shown below.
zip longest iterates across two lists

Below is the implementation:

import itertools
# initializing lists
samplelistone = [27823, 9792, "hello", "this", True, 7.6]
samplelisttwo = ['sky', 'is', 'blue', 122, 5.3214, False, 'hello']
# printing the two original lists
print("printing the samplelistone ", *samplelistone)
print("printing the samplelisttwo ", *samplelisttwo)
# traversing the two lists simulatenously
print("printing the two lists elements simultaneously ")
for i, j in itertools.zip_longest(samplelistone, samplelisttwo):
    print(i, j)

Output:

printing the samplelistone  27823 9792 hello this True 7.6
printing the samplelisttwo  sky is blue 122 5.3214 False hello
printing the two lists elements simultaneously 
27823 sky
9792 is
hello blue
this 122
True 5.3214
7.6 False
None hello

Related Programs: