
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Iterate Through Two Lists in Parallel in Python
In Python, using a for loop is common to iterate a single data structure like a list, but if we need to iterate two/multiple lists in parallel, we need to use the range() function.
Iterating two Lists Using for Loop
Assuming that both lists have the same length, here we are using the len() method to get the length of the list object.
Example
In the following example, we have iterated through the lists l1 and l2 of same size using for loop -
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 Lengths of the two Lists are not Same
Assuming that both lists are unequal, we will iterate the for loop using a range of minimum length.
Example
The list objects l1 and 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.
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])
Following is an output of the above code -
1 a 2 b 3 c
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 up to 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)
Following is an output of the above code -
1 a 2 b 3 c
Like as previous example, the zip() function also iterates the for loop up to the minimum length among the 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 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 by default, and we can specify filled values using the fillvalue parameter. Before using the zip_longest() function, we need to import the 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)
Following is an output of the above code -
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)
Following is an output of the above code -
1 a 10 2 b 11 3 c 12 4 d 13 5 e None 6 None None 7 None None