HOW TO PASS TWO OR MORE LISTS IN FOR LOOP USING PYTHON

Date: 07/12/2019

Introduction

Python has global built-ins that are handy for a regular problem. Specifically, I will explain zip() in this article.

Let’s take an example from Avengers,

Have to match the list of Avengers characters with the persons who acted in the role. For example, We will pick up some characters such as Iron Man, Captain America, Thor and Groot (newly added avenger but still he is a Guardian).

avengers_characters=["Iron Man","Captain America","Thor","Groot"]
avengers_personalities=["Robert Downey JR","Chris Evans","Chris Hemsworth","Vin diesel"]

Okay, now I want to print the matching characters with the persons who acted in the roles.

For that we have to use for loop right, Yes here it will be like

for characters,personalities in zip (avengers_characters,avengers_personalities) :
    print("{} by {}".format(characters,personalities))   

Wait a minute. Don’t get confused about what is zip and why I used it. I will explain it.


By for loop we can iterate only one list but by using zip function we can iterate multiple lists parallelly, where the first item in each passed iterator is paired together and then the second item in each passed iterator are paired together etc.. and even single list also be passed and results in a list of tuples. the format function is used to position formatting by the order of parentheses {} and value passing.

Syntax for zip:


zip(iterator1, iterator2, iterator3 …)

Here is the output:

Note:

In zip function , If passed iterators have different lengths, the iterator with the least items decides the length of the new iterator. ie If one list have 2 values and other lists have 3 values then the for loop will end in the length of 2 and other one value will be discarded.

Here is the example for mismatching length of two lists and its Output:

avengers_characters=["Iron Man","Captain America","Thor","Groot"]
avengers_personalities=["Robert Downey JR","Chris Evans","Chris Hemsworth"]

for characters,personalities in zip(avengers_characters,avengers_personalities) :
    print("{} by {}".format(characters,personalities))

To overcome this, there is a package in python called itertools in its there is function called izip_longest to perform up to the maximum length of the iterator .ie If one list has 2 values and other lists have 3 values then the for loop will execute up to the length of 3 and for missing value in a list will be filled by a default value called none

Here is the example of mismatching length of lists by using itertools and its output:

#some of the characters
avengers_characters=["Iron Man","Captain America","Thor","Groot"]
avengers_personalities=["Robert Downey JR","Chris Evans","Chris Hemsworth"]


#matching the avengers characters with the personalities who acted
for characters,personalities in itertools.izip_longest(avengers_characters,avengers_personalities) :
    print("{} by {}".format(characters,personalities))
   

Conclusion

Thank You for using pheonixsolutions.
If you like the blog share it with your friends and others also.

Leave a Reply