07-08-2020, 02:41 PM
Zip With List Output Instead of Tuple | Most Pythonic Way
Short answer: Per default, the function returns a zip object of tuples. To obtain a list of lists as an output, use the list comprehension statement that converts each tuple to a list and stores the converted lists in a new nested list object.
Intermediate Python coders know the function. But if you’re like me, you’ve often cursed the output of the zip function: first of all, it’s a zip object (and not a list), and, second, the individual zipped elements are tuples. But what if you need a list of lists as output? This article will show you the most Pythonic way of doing this.
Problem: Given a number of lists. How ot zipthe i-th elements of those lists together and obtain a listof lists?
Example: Given two lists and and you want the list of lists .
Here’s a quick overview of our solutions:
https://trinket.io/embed/python/07d4e000fe
Exercise: Create a new list and change the four methods to zip together all three lists(instead of only two).
Method 1: Generator Expression
The first method uses a generator expression and converts the resulting iterable to a list using the constructor.
This is efficient but not the most concise way of accomplishing this task.
Method 2: List Comprehension
A better way is to use list comprehension which is like a generator expression but it creates a list directly without the need to convert an iterable to a list (as in Method 1).
Method 3: For Loop and Zip
Coders who don’t like list comprehension and generator expressions (or, who don’t understand these beautiful Python features) often use a simple for loop. In the loop body, you convert each tuple in the zipobject to a list and appendthis list to the nested list.
This method is readable but less concise.
Method 4: For Loop and Indexing
This method is often used by coders who know neither the method, nor list comprehension, nor generator expressions: loop over all indices and append a new list obtained by grouping the i-th elements to the list.
However, this method is least Pythonic, lengthy, and it works only for equally-sized lists.
Exercise: What happens if the first list has more elements than the second list?
Method 5: Zip() + Map() + List()
A functional way of solving this problem is the map() function that applies a function to each element of an iterable and returns a map object. You can pass the constructor to the function to convert each tuple in the zip object to a list. You can then convert the map object to a list.
I don’t recommend this method because functional programming may be difficult to understand for many beginner coders. Guido van Rossum, the creator of Python, disliked functional programming as well.
Discussion
The most Pythonic way to create a list of lists by zipping together multiple lists is the list comprehension statement. List comprehension is fast, efficient, concise, and readable. You can also extend it to the general case by adding more lists to the zip function: . The function is also robust against lists of different lengths. In this case, the elements up to the maximal index of the shortest list are zipped.
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!
Short answer: Per default, the
Code:
zip()Code:
[list(x) for x in zip(l1, l2)]Intermediate Python coders know the
Code:
zip()Problem: Given a number of lists
Code:
l1, l2, ...Example: Given two lists
Code:
[1, 2, 3, 4]Code:
['Alice', 'Bob', 'Ann', 'Liz']Code:
[[1, 'Alice'], [2, 'Bob'], [3, 'Ann'], [4, 'Liz']]Code:
l1 = [1, 2, 3, 4]
l2 = ['Alice', 'Bob', 'Ann', 'Liz']
# ... calculate result ...
# Output: [[1, 'Alice'], [2, 'Bob'], [3, 'Ann'], [4, 'Liz']]Here’s a quick overview of our solutions:
https://trinket.io/embed/python/07d4e000fe
Exercise: Create a new list
Code:
l3Method 1: Generator Expression
The first method uses a generator expression and converts the resulting iterable to a list using the
Code:
list()Code:
l1 = [1, 2, 3, 4]
l2 = ['Alice', 'Bob', 'Ann', 'Liz'] # Method 1
zipped = list(list(x) for x in zip(l1, l2)) print(zipped)
# [[1, 'Alice'], [2, 'Bob'], [3, 'Ann'], [4, 'Liz']]This is efficient but not the most concise way of accomplishing this task.
Method 2: List Comprehension
A better way is to use list comprehension which is like a generator expression but it creates a list directly without the need to convert an iterable to a list (as in Method 1).
Code:
l1 = [1, 2, 3, 4]
l2 = ['Alice', 'Bob', 'Ann', 'Liz'] # Method 2:
zipped = [list(x) for x in zip(l1, l2)] print(zipped)
# [[1, 'Alice'], [2, 'Bob'], [3, 'Ann'], [4, 'Liz']]Coders who don’t like list comprehension and generator expressions (or, who don’t understand these beautiful Python features) often use a simple for loop. In the loop body, you convert each tuple in the zipobject to a list and appendthis list to the nested list
Code:
zippedCode:
l1 = [1, 2, 3, 4]
l2 = ['Alice', 'Bob', 'Ann', 'Liz'] # Method 3:
zipped = []
for t in zip(l1, l2): zipped.append(list(t)) print(zipped)
# [[1, 'Alice'], [2, 'Bob'], [3, 'Ann'], [4, 'Liz']]This method is readable but less concise.
Method 4: For Loop and Indexing
This method is often used by coders who know neither the
Code:
zip()Code:
l1 = [1, 2, 3, 4]
l2 = ['Alice', 'Bob', 'Ann', 'Liz'] # Method 4:
zipped = []
for i in range(len(l1)): zipped.append([l1[i], l2[i]]) print(zipped)
# [[1, 'Alice'], [2, 'Bob'], [3, 'Ann'], [4, 'Liz']]However, this method is least Pythonic, lengthy, and it works only for equally-sized lists.
Exercise: What happens if the first list has more elements than the second list?
Method 5: Zip() + Map() + List()
A functional way of solving this problem is the map() function that applies a function to each element of an iterable and returns a map object. You can pass the
Code:
list()Code:
map()Code:
l1 = [1, 2, 3, 4]
l2 = ['Alice', 'Bob', 'Ann', 'Liz'] # Method 5
print(list(map(list,zip(l1, l2))))
# [[1, 'Alice'], [2, 'Bob'], [3, 'Ann'], [4, 'Liz']]I don’t recommend this method because functional programming may be difficult to understand for many beginner coders. Guido van Rossum, the creator of Python, disliked functional programming as well.
Discussion
The most Pythonic way to create a list of lists by zipping together multiple lists is the list comprehension statement
Code:
[list(x) for x in zip(l1, l2)]Code:
[list(x) for x in zip(l1, l2, l3, ..., ln)]Code:
zip()Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
Join the free webinar now!

