Monday, November 18, 2024
Google search engine
HomeLanguagesHow to Zip two lists of lists in Python?

How to Zip two lists of lists in Python?

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:

  1. 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.
  2. Convert the resulting map object to a list and assign it to the variable res.
  3. Print the modified, zipped list res.

Below is the implementation of the above approach:

Python3




# 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]]

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




# 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]]

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




# Initialize two lists of lists
test_list1 = [[1, 3], [4, 5], [5, 6]]
test_list2 = [[7, 9], [3, 2], [3, 10]]
 
# Zip the two lists and use list comprehension to
# extend each sublist in test_list1 with the
# corresponding sublist in test_list2
res = [x.extend(y) for x, y in zip(test_list1, test_list2)]
 
# Print the original lists and the modified list
print("Original list 1:", test_list1)
print("Original list 2:", test_list2)
print("Modified list:", test_list1)
 
# This code is contributed by Edula Vinay Kumar Reddy


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.

  1. Use a loop to iterate over the sublists in both lists and concatenate them using the + operator. 
  2. We then add the concatenated sublist to the result list. 
  3. Finally, we print the modified zipped list.

Python3




# initializing lists
test_list1 = [[1, 3], [4, 5], [5, 6]]
test_list2 = [[7, 9], [3, 2], [3, 10]]
 
# creating an empty list to store the concatenated sublists
res = []
 
# iterating over the range of indices of the sublists in test_list1
for i in range(len(test_list1)):
 
    # concatenating the sublists at index i
    # in test_list1 and test_list2
    concatenated_sublist = test_list1[i] + test_list2[i]
 
    # adding the concatenated sublist to the result list
    res.append(concatenated_sublist)
 
# Printing the modified zipped list
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:

  1. Import the numpy module.
  2. Create numpy arrays from test_list1 and test_list2 using np.array() function.
  3. Concatenate the numpy arrays along the second axis (axis 1) using np.concatenate() function.
  4. Convert the concatenated numpy array to a nested list using tolist() method.
  5. Store the resulting nested list in the variable res.
  6. Print the modified zipped list.

Below is the implementation of the above approach:

Python3




import numpy as np
 
# Initializing lists
test_list1 = [[1, 3], [4, 5], [5, 6]]
test_list2 = [[7, 9], [3, 2], [3, 10]]
 
# Creating numpy arrays from test_list1 and test_list2
arr1 = np.array(test_list1)
arr2 = np.array(test_list2)
 
# Concatenating the numpy arrays along axis 1
concatenated_arr = np.concatenate((arr1, arr2), axis=1)
 
# Converting the concatenated numpy array to a nested list
res = concatenated_arr.tolist()
 
# Printing the modified zipped list
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




# Python3 code to demonstrate
# zipping lists of lists
# using list comprehension with unpacking and zip()
 
# Initializing lists
test_list1 = [[1, 3], [4, 5], [5, 6]]
test_list2 = [[7, 9], [3, 2], [3, 10]]
 
# zipping lists of lists
# using list comprehension with unpacking and zip()
res = [[*x, *y] for x, y in zip(test_list1, test_list2)]
 
# Printing result
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.

RELATED ARTICLES

Most Popular

Recent Comments