How to Zip two lists of lists in Python?


Two lists of lists can be merged in Python using the zip() function. Combining two lists of lists can be particularly valuable when dealing with tabular or multidimensional data, as it allows for combining related information from different sources. The zip() function, a powerful built-in tool in Python, facilitates this process by pairing corresponding elements from the sublists and generating a new list. In this article, we will explore how to zip two lists of lists in Python using the zip() function.

Algorithm

A general algorithm to zip two lists of lists is as follows:

  • Create two lists of lists, list1, and list2, with the same number of sublists.

  • Initialize an empty list, zipped_list, to store the zipped results.

  • Use the zip() function to iterate over corresponding sublists from list1 and list2.

  • For each iteration, zip the sublists together using the zip() function again.

  • Convert the zipped result into a list and append it to the zipped_list.

  • Repeat steps 3-5 for all sublists in list1 and list2.

  • Return the zipped_list as the final result.

Method 1 : Using Zip() function

Syntax

zip(*iterables)

Here, the zip() function takes multiple iterable objects as arguments and returns an iterator of tuples containing elements from each of the iterables.

Example 1: Zipping two list of equal length

In the below example, we have two lists, list1, and list2, each containing three sublists. The zip() function is used to iterate over the corresponding sublists from list1 and list2. For each iteration, the sublists are zipped together using the zip() function again. The zipped results are converted into lists and stored in the zipped_list.

list1 = [[1, 2], [3, 4], [5, 6]]
list2 = [['a', 'b'], ['c', 'd'], ['e', 'f']]

zipped_list = [list(zip(sublist1, sublist2)) for sublist1, sublist2 in zip(list1, list2)]

print(zipped_list)

Output

[[(1, 'a'), (2, 'b')], [(3, 'c'), (4, 'd')], [(5, 'e'), (6, 'f')]]

Example 2: Zipping two list of different length

In the below example, we have two lists, list1 and list2, but the second list has only two sublists instead of three. The zip() function will only iterate over the number of sublists that both lists have in common. In this case, it will only zip the first two sublists from both lists.

list1 = [[1, 2], [3, 4], [5, 6]]
list2 = [['a', 'b'], ['c', 'd']]

zipped_list = [list(zip(sublist1, sublist2)) for sublist1, sublist2 in zip(list1, list2)]

print(zipped_list)

Output

[[(1, 'a'), (2, 'b')], [(3, 'c'), (4, 'd')]]

Example 3: Zipping two empty lists

In the below example, both list1 and list2 are empty lists. Since there are no sublists to zip, the resulting zipped_list will also be empty.

list1 = []
list2 = []

zipped_list = [list(zip(sublist1, sublist2)) for sublist1, sublist2 in zip(list1, list2)]

print(zipped_list)

Output

[ ]

Method 2: Using list comprehension along with zip() method

List comprehensions provide a compact way to create lists based on existing lists or other iterables. By incorporating zip() within list comprehensions, we can simultaneously iterate over corresponding elements from multiple lists and generate a new list with the desired merged structure.

Syntax

merged_list = [expression for sublist1, sublist2 in zip(list1, list2)]

Here, the parameters used are as follows:

  • list1 and list2 are the two lists of lists that we want to merge.

  • sublist1 and sublist2 represent the corresponding sublists from list1 and list2 as we iterate using zip().

  • expression is the operation or transformation we want to apply to the corresponding elements of sublist1 and sublist2 to create the merged elements in the merged_list.

Example

In the below example, we have two lists, list1 and list2, each containing three sublists. By utilizing a list comprehension in conjunction with zip(), we can iterate over corresponding sublists and merge them together using the + operator. The resulting merged_list contains the combined elements from both lists.

list1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
list2 = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

merged_list = [sublist1 + sublist2 for sublist1, sublist2 in zip(list1, list2)]
print(merged_list)

Output

[[1, 2, 3, 'a', 'b', 'c'], [4, 5, 6, 'd', 'e', 'f'], [7, 8, 9, 'g', 'h', 'i']]

Conclusion

In this article, we discussed how we can zip two lists of lists in Python using the zip() method. By using the zip() function in combination with list comprehensions, we can iterate over corresponding sublists and zip them into a new list. It is important to note that the zip() function stops zipping when it reaches the end of the shorter list. Therefore, it is necessary to ensure that both lists have the same number of sublists if you want to zip all of them.

Updated on: 16-Oct-2023

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements