Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 is particularly valuable when dealing with tabular or multidimensional data, as it allows for combining related information from different sources. The zip() function pairs corresponding elements from sublists and generates a new structure.
Algorithm
A general algorithm to zip two lists of lists is as follows ?
Create two lists of lists,
list1andlist2, with the same number of sublistsInitialize an empty list to store the zipped results
Use the
zip()function to iterate over corresponding sublists from both listsFor each iteration, zip the sublists together using
zip()againConvert the zipped result into a list and append it to the result list
Return the final zipped list
Using zip() Function
Syntax
zip(*iterables)
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 Lists of Equal Length
Here we have two lists, each containing three sublists. The zip() function pairs corresponding sublists and creates tuples from their elements ?
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)
[[(1, 'a'), (2, 'b')], [(3, 'c'), (4, 'd')], [(5, 'e'), (6, 'f')]]
Example 2: Zipping Two Lists of Different Length
When lists have different lengths, zip() stops when the shorter list is exhausted ?
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)
[[(1, 'a'), (2, 'b')], [(3, 'c'), (4, 'd')]]
Example 3: Zipping Empty Lists
When both lists are empty, the result is also an empty list ?
list1 = [] list2 = [] zipped_list = [list(zip(sublist1, sublist2)) for sublist1, sublist2 in zip(list1, list2)] print(zipped_list)
[]
Using List Comprehension with Concatenation
List comprehensions provide a compact way to merge lists. Instead of creating tuples, we can concatenate sublists using the + operator ?
Syntax
merged_list = [sublist1 + sublist2 for sublist1, sublist2 in zip(list1, list2)]
Where ?
list1 and list2 are the two lists of lists to merge
sublist1 and sublist2 represent corresponding sublists from each list
+ operator concatenates the sublists together
Example
This approach combines sublists by concatenating them into single 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)
[[1, 2, 3, 'a', 'b', 'c'], [4, 5, 6, 'd', 'e', 'f'], [7, 8, 9, 'g', 'h', 'i']]
Comparison
| Method | Output Type | Best For |
|---|---|---|
zip() with tuples |
List of tuples | Pairing elements while preserving structure |
List comprehension with +
|
Concatenated lists | Merging sublists into single lists |
Conclusion
Use zip() to pair corresponding elements from two lists of lists, creating tuples. Use list comprehension with concatenation when you need to merge sublists into single combined lists. Remember that zip() stops at the shortest list.
