![]() |
|
[Tut] Zip With List Output Instead of Tuple | Most Pythonic Way - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Zip With List Output Instead of Tuple | Most Pythonic Way (/thread-96090.html) |
[Tut] Zip With List Output Instead of Tuple | Most Pythonic Way - xSicKxBot - 07-08-2020 Zip With List Output Instead of Tuple | Most Pythonic Way 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! |