06-13-2020, 02:49 AM
How to Merge Lists into a List of Tuples? [6 Pythonic Ways]
![[Image: merge-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/06/merge-1024x576.jpg)
The most Pythonic way to merge multiple lists into a list of tuples (grouping together the -th elements) is to use the function . If you store your lists in a list of lists , write to unpack all inner lists into the zip function.
The proficient use of Python’s built-in data structures is an integral part of your Python education. This tutorial shows you how you can merge multiple lists into the “column” representation—a list of tuples. By studying these six different ways, you’ll not only understand how to solve this particular problem, you’ll also become a better coder overall.
Problem: Given a number of lists, , …, . How to merge them into a list of tuples (column-wise)?
Example: Say, you want to merge the following lists
into a list of tuples
This tutorial shows you different ways to merge multiple lists into a list of tuples in Python 3. You can get a quick overview in our interactive Python shell:
https://trinket.io/embed/python/3d2e6b11e2
Exercise: Which method needs the least number of characters?
Method 1: Zip Function
The most Pythonic way that merges multiple lists into a list of tuples is the zip function. It accomplishes this in a single line of code—while being readable, concise, and efficient.
The function takes one or more iterables and aggregates them to a single one by combining the i-th values of each iterable into a tuple. For example, zip together lists and to .
Here’s the code solution:
The output is the following list of tuples:
Note that the return value of the function is a object. You need to convert it to a list using the constructor to create a list of tuples.
If you have stored the input lists in a single list of lists, the following method is best for you!
Method 2: Zip Function with Unpacking
You can use the asterisk operator to unpack all inner elements from a given list . Especially if you want to merge many different lists, this can significantly reduce the length of your code. Instead of writing , simplify to to unpack all inner lists into the zip function and accomplish the same thing!
This generates the list of tuples:
I’d consider using the function with unpacking the most Pythonic way to merge multiple lists into a list of tuples.
Method 3: List Comprehension
List comprehension is a compact way of creating lists. The simple formula is.
The example creates the list .
Related Article: You can read more about list comprehension in my ultimate guide on this blog.
You can use a straightforward list comprehension statement to merge multiple lists into a list of tuples:
The output produces the merged list of tuples:
This method is short and efficient. It may not be too readable for you if you’re a beginner coder—but advanced coders usually have no problems understanding this one-liner. If you love to learn everything there is about one-liner Python code snippets, check out my new book “Python One-Liners” (published with San Francisco Publisher NoStarch in 2020).
Method 4: Simple Loop
Sure, you can skip all the fancy Python and just use a simple loop as well! Here’s how this works:
The output is the merged list of tuples:
Especially coders who come to Python from another programming language such as Go, C++, or Java like this approach. They’re used to writing loops and they can quickly grasp what’s going on in this code snippet.
You can visualize the execution of this code snippet in the interactive widget:
https://pythontutor.com/iframe-embed.htm...nces=false
Exercise: Click “Next” to see how the memory usage unfolds when running the code.
Method 5: Enumerate
The method is considered to be better Python style in many scenarios—for example, if you want to iterate over all indices of a list. In my opinion, it’s slightly better than using . Here’s how you can use in your code to merge multiple lists into a single list of tuples:
The output is the list of tuples:
Still not satisfied? Let’s have a look at functional programming.
Method 6: Map + Lambda
With Python’s function, you can apply a specific function to each element of an iterable. It takes two arguments:
The result is a object. What many coders don’t know is that the function also allows multiple iterables. In this case, the lambda function takes multiple arguments—one for each iterable. It then creates an iterable of tuples and returns this as a result.
Here’s how you can create a list of tuples from a few given lists:
The output is the merged list of tuples:
This is both an efficient and readable way to merge multiple lists into a list of tuples. The fact that it isn’t well-known to use the function with multiple arguments doesn’t make this less beautiful.
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!
![[Image: merge-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2020/06/merge-1024x576.jpg)
The most Pythonic way to merge multiple lists
Code:
l0, l1, ..., lnCode:
iCode:
zip()Code:
zip(l0, l1, ..., ln)Code:
lstCode:
zip(*lst)Code:
l1 = [1, 2, 3]
l2 = [4, 5, 6]
l = list(zip(l1, l2))
print(l)
# [(1, 4), (2, 5), (3, 6)]The proficient use of Python’s built-in data structures is an integral part of your Python education. This tutorial shows you how you can merge multiple lists into the “column” representation—a list of tuples. By studying these six different ways, you’ll not only understand how to solve this particular problem, you’ll also become a better coder overall.
Problem: Given a number of lists
Code:
l1Code:
l2Code:
lnExample: Say, you want to merge the following lists
Code:
l0 = [0, 'Alice', 4500.00]
l1 = [1, 'Bob', 6666.66]
l2 = [2, 'Liz', 9999.99]into a list of tuples
Code:
[(0, 1, 2), ('Alice', 'Bob', 'Liz'), (4500.0, 6666.66, 9999.99)]This tutorial shows you different ways to merge multiple lists into a list of tuples in Python 3. You can get a quick overview in our interactive Python shell:
https://trinket.io/embed/python/3d2e6b11e2
Exercise: Which method needs the least number of characters?
Method 1: Zip Function
The most Pythonic way that merges multiple lists into a list of tuples is the zip function. It accomplishes this in a single line of code—while being readable, concise, and efficient.
The
Code:
zip()Code:
[1, 2, 3]Code:
[4, 5, 6]Code:
[(1,4), (2,5), (3,6)]Here’s the code solution:
Code:
l0 = [0, 'Alice', 4500.00]
l1 = [1, 'Bob', 6666.66]
l2 = [2, 'Liz', 9999.99] print(list(zip(l0, l1, l2)))The output is the following list of tuples:
Code:
[(0, 1, 2), ('Alice', 'Bob', 'Liz'), (4500.0, 6666.66, 9999.99)]Note that the return value of the
Code:
zip()Code:
zipCode:
list(...)If you have stored the input lists in a single list of lists, the following method is best for you!
Method 2: Zip Function with Unpacking
You can use the asterisk operator
Code:
*lstCode:
lstCode:
zip(lst[0], lst[1], ..., lst[n])Code:
zip(*lst)Code:
lst = [[0, 'Alice', 4500.00], [1, 'Bob', 6666.66], [2, 'Liz', 9999.99]]
print(list(zip(*lst)))This generates the list of tuples:
Code:
[(0, 1, 2), ('Alice', 'Bob', 'Liz'), (4500.0, 6666.66, 9999.99)]I’d consider using the
Code:
zip()Method 3: List Comprehension
List comprehension is a compact way of creating lists. The simple formula is
Code:
[expression + context]- Expression: What to do with each list element?
- Context: What elements to select? The context consists of an arbitrary number of andCode:
forstatements.Code:if
The example
Code:
[x for x in range(3)]Code:
[0, 1, 2]Related Article: You can read more about list comprehension in my ultimate guide on this blog.
You can use a straightforward list comprehension statement
Code:
[(l0[i], l1[i], l2[i]) for i in range(len(l0))]Code:
l0 = [0, 'Alice', 4500.00]
l1 = [1, 'Bob', 6666.66]
l2 = [2, 'Liz', 9999.99] print([(l0[i], l1[i], l2[i]) for i in range(len(l0))])The output produces the merged list of tuples:
Code:
[(0, 1, 2), ('Alice', 'Bob', 'Liz'), (4500.0, 6666.66, 9999.99)]This method is short and efficient. It may not be too readable for you if you’re a beginner coder—but advanced coders usually have no problems understanding this one-liner. If you love to learn everything there is about one-liner Python code snippets, check out my new book “Python One-Liners” (published with San Francisco Publisher NoStarch in 2020).
Method 4: Simple Loop
Sure, you can skip all the fancy Python and just use a simple loop as well! Here’s how this works:
Code:
l0 = [0, 'Alice', 4500.00]
l1 = [1, 'Bob', 6666.66]
l2 = [2, 'Liz', 9999.99] lst = []
for i in range(len(l0)): lst.append((l0[i], l1[i], l2[i]))
print(lst)The output is the merged list of tuples:
Code:
[(0, 1, 2), ('Alice', 'Bob', 'Liz'), (4500.0, 6666.66, 9999.99)]Especially coders who come to Python from another programming language such as Go, C++, or Java like this approach. They’re used to writing loops and they can quickly grasp what’s going on in this code snippet.
You can visualize the execution of this code snippet in the interactive widget:
https://pythontutor.com/iframe-embed.htm...nces=false
Exercise: Click “Next” to see how the memory usage unfolds when running the code.
Method 5: Enumerate
The
Code:
enumerate()Code:
range(len(l))Code:
enumerate()Code:
l0 = [0, 'Alice', 4500.00]
l1 = [1, 'Bob', 6666.66]
l2 = [2, 'Liz', 9999.99] lst = []
for i,x in enumerate(l0): lst.append((x,l1[i],l2[i]))
print(lst)The output is the list of tuples:
Code:
[(0, 1, 2), ('Alice', 'Bob', 'Liz'), (4500.0, 6666.66, 9999.99)]Still not satisfied? Let’s have a look at functional programming.
Method 6: Map + Lambda
With Python’s
Code:
map()- Function: In most cases, this is a lambda functionyou define on the fly. This is the function which you are going to apply to each element of an…
- Iterable: This is an iterable that you convert into a new iterable where each element is the result of the applied function.Code:
map()
The result is a
Code:
mapCode:
map()Here’s how you can create a list of tuples from a few given lists:
Code:
l0 = [0, 'Alice', 4500.00]
l1 = [1, 'Bob', 6666.66]
l2 = [2, 'Liz', 9999.99] lst = list(map(lambda x, y, z: (x, y, z), l0, l1, l2))
print(lst)The output is the merged list of tuples:
Code:
[(0, 1, 2), ('Alice', 'Bob', 'Liz'), (4500.0, 6666.66, 9999.99)]This is both an efficient and readable way to merge multiple lists into a list of tuples. The fact that it isn’t well-known to use the
Code:
map()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!

