If you’re in a hurry, here’s the short answer: use the list comprehension statement [tuple(x) for x in list]
to convert each element in your list
to a tuple. This works also for list of lists with varying number of elements.
But there’s more to it and studying the two main methods to achieve the same objective will make you a better coder. So keep reading:
Method 1: List Comprehension + tuple()
Problem: How to convert a list of lists into a list of tuples?
Example: You’ve got a list of lists [[1, 2], [3, 4], [5, 6]]
and you want to convert it into a list of tuples [(1, 2), (3, 4), (5, 6)]
.
Solution: There are different solutions to convert a list of lists to a list of tuples. Use list comprehension in its most basic form:
lst = [[1, 2], [3, 4], [5, 6]] tuples = [tuple(x) for x in lst] print(tuples) # [(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 tuple, you use the constructor tuple(x)
to create a new tuple from the list x
.
If you have three list elements per sublist, you can use the same approach with the conversion:
lst = [[1, 2, 1], [3, 4, 3], [5, 6, 5]] tuples = [tuple(x) for x in lst] print(tuples) # [(1, 2, 1), (3, 4, 3), (5, 6, 5)]
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 sublist, this approach still works beautifully:
lst = [[1], [2, 3, 4], [5, 6, 7, 8]] tuples = [tuple(x) for x in lst] print(tuples) # [(1,), (2, 3, 4), (5, 6, 7, 8)]
You see that an approach with list comprehension is the best way to convert a list of lists to a list of tuples. But are there any alternatives?
Method 2: Map Function + tuple()
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 lists into a list ot tuples using the map()
function:
lst = [[1], [2, 3, 4], [5, 6, 7, 8]] tuples = list(map(tuple, lst)) print(tuples) # [(1,), (2, 3, 4), (5, 6, 7, 8)]
Try it yourself:
The first argument of the map()
function is the tuple
function name. This tuple()
function converts each element on the given iterable lst
(the second argument) into a tuple. 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.
Related Articles
- List of Lists
- How to Convert a List of Tuples to a List of Lists
- 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: Simple For Loop
A simple way to convert a list of lists to a list of tuples is to start with an empty list. Then iterate over each list in the nested list in a simple for
loop, convert it to a tuple using the tuple()
function, and append it to the list of tuples.
Here’s an example:
lst = [[1], [2, 3, 4], [5, 6, 7, 8]] tuples = [] for x in lst: tuples.append(tuple(x)) print(tuples) # [(1,), (2, 3, 4), (5, 6, 7, 8)]
This is a non-fancy but effective way to create a list of tuples. You may want to watch the video on the list.append()
function—there’s more to it than could be expected on the surface!
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.