Python | Zipping two lists of lists
The normal zip function allows us the functionality to aggregate the values in a container. But sometimes, we have a requirement in which we require to have multiple lists and containing lists as index elements and we need to merge/zip them together. This is quite uncommon problem, but solution to it can still be handy. Let’s discuss certain ways in which solution can be devised.
Method #1 : Using map() + __add__
This problem can be solved using the map function with the addition operation. The map function performs the similar kind of function as the zip function and in this this case can help to reach to a solution.
# Python3 code to demonstrate # zipping lists of lists # using map() + __add__ # initializing lists test_list1 = [[1, 3], [4, 5], [5, 6]] test_list2 = [[7, 9], [3, 2], [3, 10]] # printing original lists print ("The original list 1 is : " + str(test_list1)) print ("The original list 2 is : " + str(test_list2)) # using map() + __add__ # zipping lists of lists res = list(map(list.__add__, test_list1, test_list2)) # printing result print ("The modified zipped list is : " + str(res)) |
Output :
The original list 1 is : [[1, 3], [4, 5], [5, 6]] The original list 2 is : [[7, 9], [3, 2], [3, 10]] The modified zipped list is : [[1, 3, 7, 9], [4, 5, 3, 2], [5, 6, 3, 10]]
Method #2 : Using itertools.chain() + zip()
This combination of these two functions can be used to perform this particular task. The chain function can be used to perform the interlist aggregation, and the intralist aggregation is done by zip function.
# Python3 code to demonstrate # zipping lists of lists # using map() + __add__ import itertools # initializing lists test_list1 = [[1, 3], [4, 5], [5, 6]] test_list2 = [[7, 9], [3, 2], [3, 10]] # printing original lists print ("The original list 1 is : " + str(test_list1)) print ("The original list 2 is : " + str(test_list2)) # using map() + __add__ # zipping lists of lists res = [list(itertools.chain(*i)) for i in zip(test_list1, test_list2)] # printing result print ("The modified zipped list is : " + str(res)) |
Output :
The original list 1 is : [[1, 3], [4, 5], [5, 6]] The original list 2 is : [[7, 9], [3, 2], [3, 10]] The modified zipped list is : [[1, 3, 7, 9], [4, 5, 3, 2], [5, 6, 3, 10]]
Recommended Posts:
- Python | Program to count number of lists in a list of lists
- Python | Difference between two lists
- Python | Merge key value lists
- Python | Union of two or more Lists
- Python | All possible permutations of N lists
- Python | Iterating two lists at once
- Python | Using 2D arrays/lists the right way
- Python | Intersection of two lists
- Python | Union of Value Lists
- Python | Dividing two lists
- Python | Maximum and Minimum value from two lists
- Python | Shuffle two lists with same order
- Print lists in Python (4 Different Ways)
- Python | Unpacking tuple of lists
- Python | Intersection of 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.



