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.
# 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 =" ") |
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.
# 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 =" ") |
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
Recommended Posts:
- Python | Delete items from dictionary while iterating
- Python | Program to count number of lists in a list of lists
- Python | Zipping two lists of lists
- Iterating over rows and columns in Pandas DataFrame
- Python | Merge key value lists
- Python | Using 2D arrays/lists the right way
- Python | All possible permutations of N lists
- Python | Difference between two lists
- Python | Union of two or more Lists
- Python | Union of Value Lists
- Python | Intersection of two lists
- Python | Dividing two lists
- Python | Intersection of multiple lists
- Python | Unpacking tuple of lists
- Python | Initializing multiple lists
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.

