Python | Iterating two lists at once

Sometimes, while working with Python list, we can have a problem in which we have to iterate over two list elements. Iterating one after another is an option, but it’s more cumbersome and a one-two liner is always recommended over that. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using loop + “+” operator
The combination of above functionalities can make our task easier. But the drawback here is that we might have to concatenate the list and hence would consume more memory than desired.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Iterating two lists at once
# using loop + "+" operator
  
# initializing lists
test_list1 = [4, 5, 3, 6, 2]
test_list2 = [7, 9, 10, 0]
  
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
  
# Iterating two lists at once
# using loop + "+" operator
# printing result 
print("The paired list contents are : ")
for ele in test_list1 + test_list2:
    print(ele, end =" ")

chevron_right


Output :



The original list 1 is : [4, 5, 3, 6, 2]
The original list 2 is : [7, 9, 10, 0]
The paired list contents are : 
4 5 3 6 2 7 9 10 0 

 

Method #2 : Using chain()
This is the method similar to above one, but it’s slightly more memory efficient as the chain() is used to perform the task and creates an iterator internally.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Iterating two lists at once
# using chain()
from itertools import chain
  
# initializing lists
test_list1 = [4, 5, 3, 6, 2]
test_list2 = [7, 9, 10, 0]
  
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
  
# Iterating two lists at once
# using chain()
# printing result 
print("The paired list contents are : ")
for ele in chain(test_list1, test_list2):
    print(ele, end =" ")

chevron_right


Output :

The original list 1 is : [4, 5, 3, 6, 2]
The original list 2 is : [7, 9, 10, 0]
The paired list contents are : 
4 5 3 6 2 7 9 10 0 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 :

Be the First to upvote.


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