Here are the ways to zip two lists of lists in Python:
- Using zip() function
- Using map() with __add__
- Using itertools.chain() with zip()
- Using a for loop
- Using numpy.array() and numpy.concatenate()
- Using list comprehension and unpacking
Method 1: Using zip() function
You can use the built-in zip() function to combine both lists. Each pair of sublists is paired together, forming tuples.
Then, the iterable returned by zip is converted into a list.
Visual Representation
Example
# Define two lists of lists
numbers_list = [[10, 20], [30, 40]]
letters_list = [['a', 'b'], ['c', 'd']]
print("Numbers List: ", numbers_list)
print("Letters List: ", letters_list)
# Zip the two lists of lists
zipped = zip(numbers_list, letters_list)
# Convert to a list to see the results
zipped_list = list(zipped)
print("Zipped List (combined pairs): ", zipped_list)
Output
Numbers List: [[10, 20], [30, 40]]
Letters List: [['a', 'b'], ['c', 'd']]
Zipped List (combined pairs): [([10, 20], ['a', 'b']), ([30, 40], ['c', 'd'])]
Method 2: Using map() with __add__
The map applies the __add__ method (which concatenates lists) to each pair of sublists.
The list() function then converts the resulting map object back into a list.
Visual Representation
Example
# Define two lists containing sublists
numbers_list = [[10, 20], [30, 40]]
letters_list = [['a', 'b'], ['c', 'd']]
# Display the original lists
print("Numbers List: ", numbers_list)
print("Letters List: ", letters_list)
# concatenate sublists from both lists
zipped_list = list(map(list.__add__, numbers_list, letters_list))
# Print the combined list
print("Zipped List: ", zipped_list)
Output
Numbers List: [[10, 20], [30, 40]]
Letters List: [['a', 'b'], ['c', 'd']]
Zipped List: [[10, 20, 'a', 'b'], [30, 40, 'c', 'd']]
Method 3: Using itertools.chain() with zip()
The zip() function pairs sublists from both lists, creating tuples of corresponding sublists. itertools.chain is then used to flatten these tuples into single sublists.
Finally, a list comprehension combines these operations to create a new list, where each element is a merged, flattened combination of the original sublists.
Visual Representation
Example
from itertools import chain
# Define two lists containing sublists
numbers_list = [[10, 20], [30, 40]]
letters_list = [['a', 'b'], ['c', 'd']]
# Display the original lists
print("Numbers List: ", numbers_list)
print("Letters List: ", letters_list)
# Combine and flatten pairs of sublists from both lists
zipped_list = [list(chain(*pair)) for pair in zip(numbers_list, letters_list)]
# Print the combined list
print("Zipped List: ", zipped_list)
Output
Numbers List: [[10, 20], [30, 40]]
Letters List: [['a', 'b'], ['c', 'd']]
Zipped List: [[10, 20, 'a', 'b'], [30, 40, 'c', 'd']]
Method 4: Using a for loop
First, you need to initialize an empty list for the concatenated sublists.
Then, use a for loop to iterate over both lists simultaneously with zip(). In each iteration, concatenate the corresponding sublists from each list and add them to the combined list.
Example
# Define two lists containing sublists
numbers_list = [[10, 20], [30, 40]]
letters_list = [['a', 'b'], ['c', 'd']]
# Display the original lists
print("Numbers List: ", numbers_list)
print("Letters List: ", letters_list)
# Initialize an empty list for the concatenated sublists
zipped_list = []
# Iterate over both lists and concatenate each pair of sublists
for num_sublist, letter_sublist in zip(numbers_list, letters_list):
zipped_list.append(num_sublist + letter_sublist)
# Print the combined list
print("Zipped List: ", zipped_list)
Output
Numbers List: [[10, 20], [30, 40]]
Letters List: [['a', 'b'], ['c', 'd']]
Zipped List: [[10, 20, 'a', 'b'], [30, 40, 'c', 'd']]
Method 5: Using numpy.array() and numpy.concatenate()
First, convert the lists to NumPy arrays and then use numpy.concatenate() along axis 1 to concatenate the corresponding sublists.
The concatenated array is then converted back to a list format and printed.
Example
import numpy as np
# Define two lists of sublists
numbers_list = [[10, 20], [30, 40]]
letters_list = [['a', 'b'], ['c', 'd']]
# Convert lists to NumPy arrays
numbers_array = np.array(numbers_list)
letters_array = np.array(letters_list)
# Concatenate arrays along the second axis (column-wise)
concatenated_array = np.concatenate((numbers_array, letters_array), axis=1)
# Convert the concatenated NumPy array back to a list
zipped_list = concatenated_array.tolist()
# Print the concatenated list
print("Zipped List: ", zipped_list)
Output
Zipped List: [['10', '20', 'a', 'b'], ['30', '40', 'c', 'd']]
Method 6: Using list comprehension and unpacking
Unpacking extracts the elements from each pair of sublists and combines them into a new sublist.
List comprehension iterates over all pairs, creating a new list containing these combined sublists.
Example
# Define two lists with sublists
numbers_list = [[10, 20], [30, 40]]
letters_list = [['a', 'b'], ['c', 'd']]
# Zip and merge sublists from both lists using list comprehension and unpacking
zipped_list = [[*x, *y] for x, y in zip(numbers_list, letters_list)]
# Display merged list
print("Zipped List: ", zipped_list)
Output
Zipped List: [[10, 20, 'a', 'b'], [30, 40, 'c', 'd']]
That’s it.