Iterating over single lists, refers to using for loops for iteration over a single element of a single list at a particular step whereas in iterating over multiple lists simultaneously, we refer using for loops for iteration over a single element of multiple lists at a particular step.
Iterate over multiple lists at a time
For better understanding of iteration of multiple lists, we are iterating over 3 lists at a time. We can iterate over lists simultaneously in ways:
- zip() : In Python 3, zip returns an iterator. zip() function stops when anyone of the list of all the lists gets exhausted. In simple words, it runs till the smallest of all the lists. Below is an implementation of the zip function and itertools.izip which iterates over 3 lists:
Python3
# Python program to iterate # over 3 lists using zip function import itertools num = [ 1 , 2 , 3 ] color = [ 'red' , 'while' , 'black' ] value = [ 255 , 256 ] # iterates over 3 lists and executes # 2 times as len(value)= 2 which is the # minimum among all the three for (a, b, c) in zip (num, color, value): print (a, b, c) |
1 red 255 2 while 256
- itertools.zip_longest() : zip_longest stops when all lists are exhausted. When the shorter iterator(s) are exhausted, zip_longest yields a tuple with None value. Below is an implementation of the itertools.zip_longest which iterates over 3 lists:
Python3
# Python program to iterate # over 3 lists using itertools.zip_longest import itertools num = [ 1 , 2 , 3 ] color = [ 'red' , 'while' , 'black' ] value = [ 255 , 256 ] # iterates over 3 lists and till all are exhausted for (a, b, c) in itertools.zip_longest(num, color, value): print (a, b, c) |
1 red 255 2 while 256 3 black None
- Output:
1 red 255 2 while 256 3 black None
We can also specify a default value instead of None in zip_longest()
Python3
# Python program to iterate # over 3 lists using itertools.zip_longest import itertools num = [ 1 , 2 , 3 ] color = [ 'red' , 'while' , 'black' ] value = [ 255 , 256 ] # Specifying default value as -1 for (a, b, c) in itertools.zip_longest(num, color, value, fillvalue = - 1 ): print (a, b, c) |
1 red 255 2 while 256 3 black -1
Time complexity: O(n), where n is the length of the longest list
Auxiliary space: O(1)
Note : Python 2.x had two extra functions izip() and izip_longest(). In Python 2.x, zip() and zip_longest() used to return list, and izip() and izip_longest() used to return iterator. In Python 3.x, there izip() and izip_longest() are not there as zip() and zip_longest() return iterator.
Another approach to iterate over multiple lists simultaneously is to use the enumerate() function. The enumerate() function allows you to iterate over a list and keep track of the current index of each element. You can use this index to access the corresponding elements in the other lists.
For example:
Python3
list1 = [ 1 , 2 , 3 ] list2 = [ 'a' , 'b' , 'c' ] list3 = [ 'x' , 'y' , 'z' ] for i, element in enumerate (list1): print (element, list2[i], list3[i]) |
1 a x 2 b y 3 c z
Time complexity: O(n), where n is the length of the longest list (in this case, n=3).
Auxiliary space: O(1), as no extra space is being used.
#Example-4:
Approach:
In this approach, the zip() function takes three generator expressions as arguments. Each generator expression yields the elements of each iterable on-the-fly, without creating a list or tuple to store the values.
Python3
for item1, item2, item3 in zip (( 1 , 2 , 3 ), ( 'a' , 'b' , 'c' ), ( True , False , True )): print (item1, item2, item3) |
1 a True 2 b False 3 c True
The time and space complexity of this code is constant time (O(1)) and constant space (O(1)).
The reason is that the code only iterates over three tuples with three elements each, and the number of iterations is fixed and does not depend on the input size. Therefore, the time complexity is constant.
Similarly, the space complexity is also constant, as the code only creates three tuples with three elements each, and the number of tuples created is also fixed and does not depend on the input size. Therefore, the space complexity is also constant.