How to Zip two lists of lists in Python?
Last Updated :
17 Dec, 2024
zip()
function typically aggregates values from containers. However, there are cases where we need to merge multiple lists of lists. In this article, we will explore various efficient approaches to Zip two lists of lists in Python. List Comprehension provides a concise way to zip two lists of lists together, making the code more readable and often more efficient than using the zip()
function with additional operations.
Example:
Python
l1 = [[1, 2], [3, 4], [5, 6]]
l2= [[7, 8], [9, 10], [11, 12]]
# Zipping list
res = [(a, b) for a, b in zip(l1, l2)]
print(res)
Output[([1, 2], [7, 8]), ([3, 4], [9, 10]), ([5, 6], [11, 12])]
Explanation:
zip()
function pairs corresponding sublists from l1
and l2
.- Combines these pairs into a list of tuples.
Let's explore more method to zip two list of list.
This method
allows us to zip two lists of different lengths, padding the shorter list with a specified default value. This ensures that both lists are iterated over completely, even if they have unequal lengths.
Example:
Python
import itertools
a= [[1, 2], [3, 4]]
b= [[5, 6], [7, 8], [9, 10]]
#Zipping with padding
res = list(itertools.zip_longest(a, b, fillvalue=[]))
print(res)
Output[([1, 2], [5, 6]), ([3, 4], [7, 8]), ([], [9, 10])]
Explanation:
zip_longest()
pairs sublists, filling missing values with []
.- l
ist()
converts the pairs into a list of tuples.
Using a Loop
This method uses a loop to iterate through corresponding sublists in two lists and concatenate them with the +
operator. The concatenated sublists are then added to a result list, which is printed at the end.
Example:
Python
a = [[1, 3], [4, 5], [5, 6]]
b = [[7, 9], [3, 2], [3, 10]]
res = []
# Iterating and concatenating sublists
for i in range(len(a)):
res.append(a[i] + b[i])
print(res)
Output[[1, 3, 7, 9], [4, 5, 3, 2], [5, 6, 3, 10]]
Explantion:
- Combines corresponding sublists from
a
and b
. - Appends the combined sublists to
res
.
Using numpy
When we are dealing with large datasets, numpy
is a great choice for efficiency. It is specifically optimized for large-scale array operations and can perform zipping faster than standard Python lists when the data is numeric.
Example:
Python
import numpy as np
l1 = [[1, 2], [3, 4], [5, 6]]
l2 = [[7, 8], [9, 10], [11, 12]]
# Convert lists to numpy arrays
res = np.array([np.array([a, b]) for a, b in zip(l1, l2)])
print(res)
Output[[[ 1 2]
[ 7 8]]
[[ 3 4]
[ 9 10]]
[[ 5 6]
[11 12]]]
Explanation:
- Pairs and converts sublists from
l1
and l2
into NumPy arrays. - Combines them into a 2D NumPy array.
Similar Reads
Concatenate two list of lists Row-wise-Python The task of concatenate two lists of lists row-wise, meaning we merge corresponding sublists into a single sublist. For example, given a = [[4, 3], [1, 2]] and b = [[7, 5], [9, 6]], we pair elements at the same index: [4, 3] from a is combined with [7, 5] from b, resulting in [4, 3, 7, 5], and [1, 2
3 min read
How to iterate two lists in parallel - Python Whether the lists are of equal or different lengths, we can use several techniques such as using the zip() function, enumerate() with indexing, or list comprehensions to efficiently iterate through multiple lists at once. Using zip() to Iterate Two Lists in ParallelA simple approach to iterate over
2 min read
Merge Two Lists into List of Tuples - Python The task of merging two lists into a list of tuples involves combining corresponding elements from both lists into paired tuples. For example, given two lists like a = [1, 2, 3] and b = ['a', 'b', 'c'], the goal is to merge them into a list of tuples, resulting in [(1, 'a'), (2, 'b'), (3, 'c')]. Usi
3 min read
How to compare two lists in Python? In Python, there might be a situation where you might need to compare two lists which means checking if the lists are of the same length and if the elements of the lists are equal or not. Let us explore this with a simple example of comparing two lists.Pythona = [1, 2, 3, 4, 5] b = [1, 2, 3, 4, 5] #
3 min read
Convert List of Tuples To Multiple Lists in Python When working with data in Python, it's common to encounter situations where we need to convert a list of tuples into separate lists. For example, if we have a list of tuples where each tuple represents a pair of related data points, we may want to split this into individual lists for easier processi
3 min read
Python - Convert a list into tuple of lists When working with data structures in Python, there are times when we need to convert a list into a tuple of smaller lists.For example, given a list [1, 2, 3, 4, 5, 6], we may want to split it into a tuple of two lists like ([1, 2, 3], [4, 5, 6]). We will explore different methods to achieve this con
3 min read