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 | Zipping two unequal length list in dictionary
- Python | Program to count number of lists in a list of lists
- Python - Convert Lists into Similar key value lists
- Creating a sorted merged list of two unsorted lists in Python
- Python | Intersection of two lists
- Python | Union of two or more Lists
- Python | Difference between two lists
- Python | Check if two lists have at-least one element common
- Python | Check whether two lists are circularly identical
- Python | Print all the common elements of two lists
- Python | Split the Even and Odd elements into two different lists
- Python | Find missing and additional values in two lists
- Python | Average of two lists
- Python | Ways to concatenate two lists
- Python | Check if two lists are identical
- Python | Convert two lists into a dictionary
- Python | Combining two sorted lists
- Python | Merge two lists into list of tuples
- Python | Concatenate two lists element-wise
- Python | Integrity Sorting in two 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.

