Write a python program to unzip a list of tuples into individual lists.

Use the list comprehension statement [list(x) for x in tuples] to convert each tuple in tuples to a list. This also works for a list of tuples with a varying number of elements.

How to Convert List of Lists to List of Tuples in Python? (And Back)

Write a python program to unzip a list of tuples into individual lists.

Watch this video on YouTube

But there’s more to it, and studying the three main methods to achieve the same goal will make you a better coder. So keep reading! 👓

Method 1: List Comprehension + list()

Problem: How to convert a list of tuples into a list of lists?

Example: You’ve got a list of tuples [(1, 2), (3, 4), (5, 6)] and you want to convert it into a list of lists [[1, 2], [3, 4], [5, 6]].

Write a python program to unzip a list of tuples into individual lists.

Solution: There are different solutions to convert a list of tuples to a list of lists. The recommended way is to use list comprehension in its most basic form:

tuples = [(1, 2), (3, 4), (5, 6)]
lists = [list(x) for x in tuples]
print(lists)
# [[1, 2], [3, 4], [5, 6]]

Try It Yourself:

This approach is simple and effective. List comprehension defines how to convert each value (x in the example) to a new list element.

You use the constructor

tuples = [(1, 2, 1), (3, 4, 3), (5, 6, 5)]
lists = [list(x) for x in tuples]
print(lists)
0 to create a new list from the tuple x.

Python list() — A Simple Guide

Write a python program to unzip a list of tuples into individual lists.

Watch this video on YouTube

If you have three list elements per tuple, you can use the same approach with the conversion:

tuples = [(1, 2, 1), (3, 4, 3), (5, 6, 5)]
lists = [list(x) for x in tuples]
print(lists)

You can see the execution flow in the following interactive visualization (just click the “Next” button to see what’s happening in the code):

And if you have a varying number of list elements per tuple, this approach still works beautifully:

tuples = [(1,), (3, 3), (5, 6, 5)]
lists = [list(x) for x in tuples]
print(lists)
# [[1], [3, 3], [5, 6, 5]]

You see that an approach with list comprehension is the best way to convert a list of tuples to a list of lists. But are there any alternatives?

Method 2: Map Function + list()

An alternative is to use the

tuples = [(1, 2, 1), (3, 4, 3), (5, 6, 5)]
lists = [list(x) for x in tuples]
print(lists)
2 function that applies a specified function on each element of an iterable.

💡 Side note: Guido van Rossum, the creator of Python, didn’t like the

tuples = [(1, 2, 1), (3, 4, 3), (5, 6, 5)]
lists = [list(x) for x in tuples]
print(lists)
3 function as it’s less readable (and less efficient) than the list comprehension version (method 1 in this tutorial). Feel free to read a detailed discussion on how exactly he argued in my blog article.

So, without further ado, here’s how you can convert a list of tuples into a list of lists using the

tuples = [(1, 2, 1), (3, 4, 3), (5, 6, 5)]
lists = [list(x) for x in tuples]
print(lists)
3 function:

tuples = [(1,), (2, 3, 4), (5, 6, 7, 8)]
lists = list(map(list, tuples))
print(lists)
# [[1], [2, 3, 4], [5, 6, 7, 8]]

Try it yourself:

The first argument of the

tuples = [(1, 2, 1), (3, 4, 3), (5, 6, 5)]
lists = [list(x) for x in tuples]
print(lists)
3 function is the
tuples = [(1, 2, 1), (3, 4, 3), (5, 6, 5)]
lists = [list(x) for x in tuples]
print(lists)
6 function name.

This

tuples = [(1, 2, 1), (3, 4, 3), (5, 6, 5)]
lists = [list(x) for x in tuples]
print(lists)
7 function converts each element on the given iterable tuples (the second argument) into a list.

The result of the

tuples = [(1, 2, 1), (3, 4, 3), (5, 6, 5)]
lists = [list(x) for x in tuples]
print(lists)
3 function is an iterable, so you need to convert it to a list before printing it to the shell because the default string representation of an iterable is not human-readable.

Related Articles

  • List of Lists
  • How to Convert a List of Lists to a List of Tuples
  • How to Convert a List of Lists to a Pandas Dataframe
  • How to Convert a List of Lists to a NumPy Array
  • How to Convert a List of Lists to a Dictionary in Python

Method 3: Use Asterisk and List Comprehension

A variant of the recommended way to convert a list of tuples to a list of lists is using list comprehension in combination with the unpacking asterisk operator

tuples = [(1,), (3, 3), (5, 6, 5)]
lists = [list(x) for x in tuples]
print(lists)
# [[1], [3, 3], [5, 6, 5]]
0 like so:
tuples = [(1,), (3, 3), (5, 6, 5)]
lists = [list(x) for x in tuples]
print(lists)
# [[1], [3, 3], [5, 6, 5]]
1.

Here’s an example:

tuples = [(1,), (3, 3), (5, 6, 5)]
lists = [[*x] for x in tuples]

print(lists)
# [[1], [3, 3], [5, 6, 5]]

The unpacking operator

tuples = [(1,), (3, 3), (5, 6, 5)]
lists = [list(x) for x in tuples]
print(lists)
# [[1], [3, 3], [5, 6, 5]]
2 takes all tuple elements from x and “unpacks” them in the outer list container
tuples = [(1,), (3, 3), (5, 6, 5)]
lists = [list(x) for x in tuples]
print(lists)
# [[1], [3, 3], [5, 6, 5]]
4. For example, the expression
tuples = [(1,), (3, 3), (5, 6, 5)]
lists = [list(x) for x in tuples]
print(lists)
# [[1], [3, 3], [5, 6, 5]]
5 yields the list
tuples = [(1,), (3, 3), (5, 6, 5)]
lists = [list(x) for x in tuples]
print(lists)
# [[1], [3, 3], [5, 6, 5]]
6.

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. 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?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming 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.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Write a python program to unzip a list of tuples into individual lists.

Chris

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.

How to unzip a list of tuples into individual lists in Python?

This task can be achieved by various methods. Let's discuss some of the methods to unzip a list of tuples. Method #1 : Using List Comprehension Using list comprehension is the most naive approach to perform this task of unzipping and usually not used to perform this task, but good method to start with.

How to convert list of tuples into list of lists in Python?

If you're in a hurry, here's the short answer: Use the list comprehension statement [list(x) for x in tuples] to convert each tuple in tuples to a list. This also works for a list of tuples with a varying number of elements.

How do I unpack a list of tuples in Python?

Basics of unpacking a tuple and a list. If you write variables on the left side separated by commas , , elements of a tuple or list on the right side are assigned to each variable. ... .
Unpack with _ (underscore) By convention, unnecessary values may be assigned to underscores _ in Python. ... .
Unpack with * (asterisk).

How do I make a list of tuples into one list?

Given a list of tuples, write a Python program to convert it into list..
Examples:.
Method #1: Using list comprehension..
Method #2: Using itertools..
Method #3: Using iteration..
Method #4: Using sum..
Method #5: Using operator and reduce..
Method #6: Using lambda..
Method #6: Using enumerate function..