Python Iterating over multiple sequence at a time

We have seen in the previous post that using Python for loop we can iterate over any sequence easily and terminate when the sequence run out of items.But you should know that using only for allows us to iterate over one sequence at a time.So what do we do if we want to iterate over multiple sequence at a time.

1st method:Using index postion

The usual method to access multiple sequence is to shown below.

Link : Python range()

>>> employee=['John' , 'Newman' , 'Luffy' , 'Naruto' , 'Ichigo']
>>> salary=[ '23000' , '450$' , 'Pirate king' , 'Holage' , 'Shinigami']
>>> for num in range( len(employee) ):
	print( employee[num] , ':' , salary[num] )

John : 23000
Newman : 450$
Luffy : Pirate king
Naruto : Holage
Ichigo : Shinigami

The method is simple,the len(employee) returns 5,which is the number of items in employee, and range( ) loop through the number from 0 to 4 since 5(return by len(employee)) is provided as argument to range().

The disadvantage of this method is that each time we want to access the items in employee and salary we have to locate it using the index position.Locating an item using the index position in a sequence is inefficient,each time we want to locate a new item using the new index position we will have to iterate the entire sequence again and again.Thus using index position to access the item is discourage.

2nd method:Using zip() (most efficient method)

The 2nd method of iterating over multiple sequence is done using the Python built-in function known as zip() function.The zip() function allow iterating over any number of sequence at a time,and of course we still need ‘for’ loop for this too.

The format of using zip() with for loop to iterate over multiple sequence is:
for name1 , name2 in zip( sequence1, sequence2 )
The sequence1 and sequence2 are the sequences from which the item is to be taken.
name1 and name2 are the name of the object that store the items taken from the sequence1 and sequence2.

More about zip(): Python zip() function

Consider the code below.

>>> employee=['John' , 'Newman' , 'Luffy' , 'Naruto' , 'Ichigo']
>>> salary=[ '23000' , '450$' , 'Pirate king' , 'Holage' , 'Shinigami']
>>> for emp , sal in zip( employee , salary ):
	print( emp , ":" , sal )

John : 23000
Newman : 450$
Luffy : Pirate king
Naruto : Holage
Ichigo : Shinigami

Now this method is the most efficient method it does not involve any using of index position to access the item.

Using zip() we can iterate over any number of sequence we want.

>>> number =[1 ,2 ,3 , 4 ,5 ]
>>> number_words=('One' , 'Two' , 'Three' , 'Four' , 'Five')
>>> roman_numeral=['I' , 'II' , 'III' , 'IV' , 'V']
>>> for num , num_word, rom_num in zip( number , number_words , roman_numeral ):
	print( num , '-' , num_word , ":" , rom_num )

1 - One : I
2 - Two : II
3 - Three : III
4 - Four : IV
5 - Five : V

The ‘number_words’ is tuple but ‘number’ and ‘roman_numeral’ are list but the program still works fine.

The only thing to consider when using this method is that the sequence must have equal number of items.If unequal number of items is found the zip() will take into account the sequence with the smallest number of items,anything else is neglected.

>>> seq=[23 , 78 , 90 , 9]
>>> se1=['Twenty-three' , 'Seventy=eight' , 'Ninety']
>>> for num , numW in zip ( seq , se1 ):
	print( num ,":" , numW )

23 : Twenty-three
78 : Seventy=eight
90 : Ninety

The fourth item of ‘seq’ is discarded.

Leave a Reply

Your email address will not be published. Required fields are marked *