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 element in your
tuples list to a list. This works also for list of tuples with varying number of elements.
But there’s more to it and studying the two main method to achieve the same objective 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]].
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. As each list element is a new list, you use the constructor
list(x) to create a new tuple from the list
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) # [, [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 map function that applies a specified function on each element of an iterable. Guido van Rossum, the creator of Python, didn’t like the
map() function as it’s less readable (and less efficient) than the list comprehension version (method 1 in this tutorial). You can read about a detailed discussion on how exactly he argued on my blog article.
So, without further ado, here’s how you can convert a list of tuples into a list ot lists using the
tuples = [(1,), (2, 3, 4), (5, 6, 7, 8)] lists = list(map(list, tuples)) print(lists) # [, [2, 3, 4], [5, 6, 7, 8]]
Try it yourself:
The first argument of the
map() function is the
list function name. This
list() function converts each element on the given iterable
tuples (the second argument) into a list. The result of the
map() 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.
- 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
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.
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.