Python | Iterate over multiple lists simultaneously

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:

  1. zip()and izip() : In Python, zip returns a list of tuples. This is fine when lists are not massive. If they are massive then forming zip(list1, list2, ….) is an unnecessarily massive temporary variable, and should be replaced by itertools.izip or itertools.izip_longest, which returns an iterator instead of a list. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python progam to iterate
    # over 3 lists using zip and izip function
      
    import itertools 
      
    num = [1, 2, 3]
    color = ['red', 'while', 'black']
    value = [255, 256]
      
    # iterates over 3 lists and excutes 
    # 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
      
    print "\niterating using izip"
    for (a, b, c) in itertools.izip(num, color, value):
        print a, b, c

    chevron_right

    
    

    Output:

    1 red 255
    2 while 256
    
    iterating using izip
    1 red 255
    2 while 256
    
  2. itertools.izip_longest() : izip_longest stops when all lists are exhausted. When the shorter iterator(s) are exhausted, izip_longest yields a tuple with None value.

    Below is an implementation of the itertools.izip_longest which iterates over 3 lists:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Python progam to iterate
    # over 3 lists using zip and izip function
      
    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.izip_longest(num, color, value):
        print a, b, c

    chevron_right

    
    

    Output:

    1 red 255
    2 while 256
    3 black None
    


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.