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 solutions 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 case can help to reach to a solution.
Approach:
- Use the map() function to apply the list.__add__() method to each pair of sub-lists in test_list1 and test_list2, resulting in a new list of lists that combines the elements of each pair of sub-lists.
- Convert the resulting map object to a list and assign it to the variable res.
- Print the modified, zipped list res.
Below is the implementation of the above approach:
Python3
test_list1 = [[ 1 , 3 ], [ 4 , 5 ], [ 5 , 6 ]]
test_list2 = [[ 7 , 9 ], [ 3 , 2 ], [ 3 , 10 ]]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = list ( map ( list .__add__, test_list1, test_list2))
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]]
Time complexity: O(n), where n is the length of the longest list among test_list1 and test_list2.
Auxiliary space: O(n), where n is the length of the longest list among test_list1 and test_list2.
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
import itertools
test_list1 = [[ 1 , 3 ], [ 4 , 5 ], [ 5 , 6 ]]
test_list2 = [[ 7 , 9 ], [ 3 , 2 ], [ 3 , 10 ]]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = [ list (itertools.chain( * i))
for i in zip (test_list1, test_list2)]
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]]
Time complexity: O(n), where n is the length of the input lists.
Auxiliary space: O(n), where n is the length of the input lists.
Method #3: Using list comprehension with extend()
This code initializes two lists of lists, test_list1 and test_list2. It then uses the built-in zip function to pair up the sublists from each list and uses list comprehension to iterate over the pairs of sublists. For each pair, it calls the extend() method on the first sublist to add the elements of the second sublist to it. Finally, it prints the original lists and the modified list.
Time complexity: O(n), where n is the total number of elements in the lists.
Auxiliary Space: O(n)
Python3
test_list1 = [[ 1 , 3 ], [ 4 , 5 ], [ 5 , 6 ]]
test_list2 = [[ 7 , 9 ], [ 3 , 2 ], [ 3 , 10 ]]
res = [x.extend(y) for x, y in zip (test_list1, test_list2)]
print ( "Original list 1:" , test_list1)
print ( "Original list 2:" , test_list2)
print ( "Modified list:" , test_list1)
|
Output
Original list 1: [[1, 3, 7, 9], [4, 5, 3, 2], [5, 6, 3, 10]]
Original list 2: [[7, 9], [3, 2], [3, 10]]
Modified list: [[1, 3, 7, 9], [4, 5, 3, 2], [5, 6, 3, 10]]
Time complexity: O(n).
Auxiliary space: O(1).
Method #4: Using a loop to iterate over both lists and concatenate the sublists.
- Use a loop to iterate over the sublists in both lists and concatenate them using the + operator.
- We then add the concatenated sublist to the result list.
- Finally, we print the modified zipped list.
Python3
test_list1 = [[ 1 , 3 ], [ 4 , 5 ], [ 5 , 6 ]]
test_list2 = [[ 7 , 9 ], [ 3 , 2 ], [ 3 , 10 ]]
res = []
for i in range ( len (test_list1)):
concatenated_sublist = test_list1[i] + test_list2[i]
res.append(concatenated_sublist)
print ( "The modified zipped list is:" , res)
|
Output
The modified zipped list is: [[1, 3, 7, 9], [4, 5, 3, 2], [5, 6, 3, 10]]
Time complexity: O(n).
Auxiliary space: O(n).
Method #5: Using numpy.array() and numpy.concatenate()
Approach:
- Import the numpy module.
- Create numpy arrays from test_list1 and test_list2 using np.array() function.
- Concatenate the numpy arrays along the second axis (axis 1) using np.concatenate() function.
- Convert the concatenated numpy array to a nested list using tolist() method.
- Store the resulting nested list in the variable res.
- Print the modified zipped list.
Below is the implementation of the above approach:
Python3
import numpy as np
test_list1 = [[ 1 , 3 ], [ 4 , 5 ], [ 5 , 6 ]]
test_list2 = [[ 7 , 9 ], [ 3 , 2 ], [ 3 , 10 ]]
arr1 = np.array(test_list1)
arr2 = np.array(test_list2)
concatenated_arr = np.concatenate((arr1, arr2), axis = 1 )
res = concatenated_arr.tolist()
print ( "The modified zipped list is:" , res)
|
Output
The modified zipped list is: [[1, 3, 7, 9], [4, 5, 3, 2], [5, 6, 3, 10]]
Time complexity: O(n), where n is the total number of elements in the input lists.
Auxiliary space: O(n), where n is the total number of elements in the input lists.
Method #6: Using list comprehension with unpacking and zip()
Python3
test_list1 = [[ 1 , 3 ], [ 4 , 5 ], [ 5 , 6 ]]
test_list2 = [[ 7 , 9 ], [ 3 , 2 ], [ 3 , 10 ]]
res = [[ * x, * y] for x, y in zip (test_list1, test_list2)]
print ( "The modified zipped list is : " + str (res))
|
Output
The modified zipped list is : [[1, 3, 7, 9], [4, 5, 3, 2], [5, 6, 3, 10]]
Time complexity: O(n), where n is the total number of elements in the input lists.
Auxiliary space: O(n), where n is the total number of elements in the input lists.
Please Login to comment...