How to Merge Lists into a List of Tuples? [6 Pythonic Ways]

Merge Lists to List of Tuples

The most Pythonic way to merge multiple lists l0, l1, ..., ln into a list of tuples (grouping together the i-th elements) is to use the zip() function zip(l0, l1, ..., ln). If you store your lists in a list of lists lst, write zip(*lst) to unpack all inner lists into the zip function.

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 l1, l2, …, ln. How to merge them into a list of tuples (column-wise)?

Example: Say, you want to merge the following lists

l0 = [0, 'Alice', 4500.00]
l1 = [1, 'Bob', 6666.66]
l2 = [2, 'Liz', 9999.99]

into a list of tuples

[(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:

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 zip() 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 [1, 2, 3] and [4, 5, 6] to [(1,4), (2,5), (3,6)].

Here’s the code solution:

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:

[(0, 1, 2), ('Alice', 'Bob', 'Liz'), (4500.0, 6666.66, 9999.99)]

Note that the return value of the zip() function is a zip object. You need to convert it to a list using the list(...) 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 *lst to unpack all inner elements from a given list lst. Especially if you want to merge many different lists, this can significantly reduce the length of your code. Instead of writing zip(lst[0], lst[1], ..., lst[n]), simplify to zip(*lst) to unpack all inner lists into the zip function and accomplish the same thing!

lst = [[0, 'Alice', 4500.00],
       [1, 'Bob', 6666.66],
       [2, 'Liz', 9999.99]]
print(list(zip(*lst)))

This generates the list of tuples:

[(0, 1, 2), ('Alice', 'Bob', 'Liz'), (4500.0, 6666.66, 9999.99)]

I’d consider using the zip() 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 [expression + context].

  • Expression: What to do with each list element?
  • Context: What elements to select? The context consists of an arbitrary number of for and if statements.

The example [x for x in range(3)] creates the list [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 [(l0[i], l1[i], l2[i]) for i in range(len(l0))] to merge multiple lists into a list of tuples:

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:

[(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:

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:

[(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:

Exercise: Click “Next” to see how the memory usage unfolds when running the code.

Method 5: Enumerate

The enumerate() 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 range(len(l)). Here’s how you can use enumerate() in your code to merge multiple lists into a single list of tuples:

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:

[(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 map() function, you can apply a specific function to each element of an iterable. It takes two arguments:

  • Function: In most cases, this is a lambda function you 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 map() function.

The result is a map object. What many coders don’t know is that the map() 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:

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:

[(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 map() 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!