# How to Zip Two Lists of Lists in Python

Here are the ways to zip two lists of lists in Python:

1. Using map() + __add__
2. Using itertools.chain() + zip()
3. Using list comprehension with extend()
4. Using a loop to iterate over both lists and concatenate the sublists
5. Using numpy.array() and numpy.concatenate()
6. Using list comprehension with unpacking and zip()

## Method 1: Using map() + __add__

``````list1 = [[11, 31], [41, 51], [15, 16]]
list2 = [[71, 91], [31, 21], [31, 10]]

print("The original list 1 is : " + str(list1))
print("The original list 2 is : " + str(list2))

res = list(map(list.__add__, list1, list2))

print("The modified zipped list is : " + str(res))
``````

Output

``````The original list 1 is : [[11, 31], [41, 51], [15, 16]]
The original list 2 is : [[71, 91], [31, 21], [31, 10]]
The modified zipped list is : [[11, 31, 71, 91], [41, 51, 31, 21], [15, 16, 31, 10]]
``````

## Method 2: Using itertools.chain() + zip()

``````import itertools

list1 = [[11, 31], [41, 51], [15, 16]]
list2 = [[71, 91], [31, 21], [31, 10]]

print("The original list 1 is : " + str(list1))
print("The original list 2 is : " + str(list2))

res = [list(itertools.chain(*i))
for i in zip(list1, list2)]

print("The modified zipped list is : " + str(res))
``````

Output

``````The original list 1 is : [[11, 31], [41, 51], [15, 16]]
The original list 2 is : [[71, 91], [31, 21], [31, 10]]
The modified zipped list is : [[11, 31, 71, 91], [41, 51, 31, 21], [15, 16, 31, 10]]
``````

## Method 3: Using list comprehension with extend()

``````import itertools

list1 = [[11, 31], [41, 51], [15, 16]]
list2 = [[71, 91], [31, 21], [31, 10]]

res = [x.extend(y) for x, y in zip(list1, list2)]

print("Original list 1:", list1)
print("Original list 2:", list2)
print("Modified list:", list1)
``````

Output

``````Original list 1: [[11, 31, 71, 91], [41, 51, 31, 21], [15, 16, 31, 10]]
Original list 2: [[71, 91], [31, 21], [31, 10]]
Modified list: [[11, 31, 71, 91], [41, 51, 31, 21], [15, 16, 31, 10]]
``````

## Method 4: Using a loop to iterate over both lists and concatenate the sublists

``````import itertools

list1 = [[11, 31], [41, 51], [15, 16]]
list2 = [[71, 91], [31, 21], [31, 10]]

res = []

for i in range(len(list1)):
concatenated_sublist = list1[i] + list2[i]
res.append(concatenated_sublist)

print("The modified zipped list is:", res)
``````

Output

``The modified zipped list is: [[11, 31, 71, 91], [41, 51, 31, 21], [15, 16, 31, 10]]``

## Method 5: Using numpy.array() and numpy.concatenate()

``````import itertools
import numpy as np

list1 = [[11, 31], [41, 51], [15, 16]]
list2 = [[71, 91], [31, 21], [31, 10]]

arr1 = np.array(list1)
arr2 = np.array(list2)

# Concatenating the numpy arrays along axis 1
concatenated_arr = np.concatenate((arr1, arr2), axis=1)

res = concatenated_arr.tolist()

print(res)``````

Output

``[[11, 31, 71, 91], [41, 51, 31, 21], [15, 16, 31, 10]]``

## Method 6: Using list comprehension with unpacking and zip()

``````list1 = [[11, 31], [41, 51], [15, 16]]
list2 = [[71, 91], [31, 21], [31, 10]]

res = [[*x, *y] for x, y in zip(list1, list2)]

# Printing result
print("The modified zipped list is : " + str(res))
``````

Output

``The modified zipped list is : [[11, 31, 71, 91], [41, 51, 31, 21], [15, 16, 31, 10]]``

That’s it.

This site uses Akismet to reduce spam. Learn how your comment data is processed.