How to Zip two lists of lists in Python? Last Updated : 17 Dec, 2024 Comments Improve Suggest changes Like Article Like Report 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.Table of ContentUsing itertools.zip_longestUsing a LoopUsing numpyUsing itertools.zip_longestThis 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 [].list() converts the pairs into a list of tuples.Using a LoopThis 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 numpyWhen 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. Comment More infoAdvertise with us Next Article How to Zip two lists of lists in Python? manjeet_04 Follow Improve Article Tags : Python Python Programs python-list Python list-programs Marketing +1 More Practice Tags : pythonpython-list 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 Python | Split nested list into two lists Given a nested 2D list, the task is to split the nested list into two lists such that the first list contains the first elements of each sublist and the second list contains the second element of each sublist. In this article, we will see how to split nested lists into two lists in Python. Python Sp 5 min read Python | Convert list of tuples to list of list Converting list of tuples to list of lists in Python is a task where each tuple is transformed into list while preserving its elements. This operation is commonly used when we need to modify or work with the data in list format instead of tuples.Using numpyNumPy makes it easy to convert a list of tu 3 min read How to Join a list of tuples into one list? In Python, we may sometime need to convert a list of tuples into a single list containing all elements. Which can be done by several methods. The simplest way to join a list of tuples into one list is by using nested for loop to iterate over each tuple and then each element within that tuple. Let's 2 min read Python | Convert list of tuples into list In Python we often need to convert a list of tuples into a flat list, especially when we work with datasets or nested structures. In this article, we will explore various methods to Convert a list of tuples into a list. Using itertools.chain() itertools.chain() is the most efficient way to flatten a 3 min read Like