06-02-2022, 08:39 AM
How to Convert List of Lists to Tuple of Tuples in Python?
5/5 – (1 vote)
Question: Given a list of lists such as . How to convert it to a tuple of tuples such as ?
If you’re in a hurry, here’s the most Pythonic way to convert a nested list to a nested tuple:
Use a generator expression with the built-in function to convert a list of lists to a tuple of tuples like so: .
Here’s a graphic on how to convert back and forth between nested list and nested tuples:
![[Image: list_of_lists_to_tuple_of_tuples-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2022/05/list_of_lists_to_tuple_of_tuples-1024x576.jpg)
But there’s more to it! Studying the different methods to achieve the same goal will make you a better coder.![[Image: 1f9d1-200d-1f4bb.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f9d1-200d-1f4bb.png)
So keep reading!
Method 1: Tuple Comprehension + tuple()
The recommended way to convert a list of lists to a tuple of tuples is using generator expression in combination with the built-in function like so: .
Here’s a concrete example:
Try It Yourself:
https://trinket.io/embed/python/98fd39ea7a
This approach is simple and effective. The generator expression defines how to convert each inner list ( in the example) to a new tuple element.
You use the constructor to create a new tuple from the list .
Example Three Elements per Tuple
If you have three elements per list, you can use the same approach with the conversion:
You can see the execution flow in the following interactive visualization (just click the “Next” button to see what’s happening in the code):
https://pythontutor.com/iframe-embed.htm...nces=false
Example Varying Number of List Elements
And if you have a varying number of elements per list, this approach still works beautifully:
You see that an approach with generator expression is the best way to convert a list of lists to a tuple of tuples.
But are there any alternatives? Let’s have a look at a completely different approach to solve this problem:
Method 2: Map Function + list()
Use the map 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 function as it’s less readable and less efficient than the generator expression 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 tuple of tuples using the function:
Try it yourself:
https://trinket.io/embed/python/988e6b062f
Video tutorial on the function:
The first argument of the function is the function name.
This function converts each element on the given iterable (the second argument) into a tuple.
The result of the function is an iterable too, so you need to convert it to a tuple before printing it to the shell because the default string representation of an iterable is not human-readable.
Method 3: Simple For Loop with append() and tuple()
To convert a list of lists to a tuple of tuples, first initialize an empty “outer” list and store it in a variable.
Then iterate over all lists using a simple loop and convert each separately to a tuple.
Next, append each result to the outer list variable using the builtin method in the loop body.
Finally, convert the list of tuples to a list tuple of tuples using the function.
The following example does exactly that:
Related Video Tutorial
Related Conversion Articles
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!
5/5 – (1 vote)
Question: Given a list of lists such as Code:
[[1, 2], [3, 4]]Code:
((1, 2), (3, 4))If you’re in a hurry, here’s the most Pythonic way to convert a nested list to a nested tuple:
Use a generator expression with the built-in
Code:
tuple()Code:
tuple(tuple(x) for x in my_list)Here’s a graphic on how to convert back and forth between nested list and nested tuples:
![[Image: list_of_lists_to_tuple_of_tuples-1024x576.jpg]](https://blog.finxter.com/wp-content/uploads/2022/05/list_of_lists_to_tuple_of_tuples-1024x576.jpg)
But there’s more to it! Studying the different methods to achieve the same goal will make you a better coder.
![[Image: 1f9d1-200d-1f4bb.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f9d1-200d-1f4bb.png)
So keep reading!
Method 1: Tuple Comprehension + tuple()
The recommended way to convert a list of lists to a tuple of tuples is using generator expression in combination with the built-in
Code:
tuple()Code:
tuple(tuple(x) for x in my_list)Here’s a concrete example:
Code:
lists = [[1, 2], [3, 4], [5, 6]]
tuples = tuple(tuple(x) for x in lists) print(tuples)
# ((1, 2), (3, 4), (5, 6))Try It Yourself:
https://trinket.io/embed/python/98fd39ea7a
This approach is simple and effective. The generator expression defines how to convert each inner list (
Code:
xYou use the constructor
Code:
tuple(x)Code:
xExample Three Elements per Tuple
If you have three elements per list, you can use the same approach with the conversion:
Code:
lists = [[1, 2, 1], [3, 4, 3], [5, 6, 5]]
tuples = tuple(tuple(x) for x in lists) 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):
https://pythontutor.com/iframe-embed.htm...nces=false
Example Varying Number of List Elements
And if you have a varying number of elements per list, this approach still works beautifully:
Code:
lists = [[1], [2, 4, 3], [6, 5]]
tuples = tuple(tuple(x) for x in lists) print(tuples)
# ((1,), (2, 4, 3), (6, 5))You see that an approach with generator expression is the best way to convert a list of lists to a tuple of tuples.
But are there any alternatives? Let’s have a look at a completely different approach to solve this problem:
Method 2: Map Function + list()
Use the map 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 Code:
map()So, without further ado, here’s how you can convert a list of lists into a tuple of tuples using the
Code:
map()Code:
lists = [[1], [2, 4, 3], [6, 5]]
tuples = tuple(map(tuple, lists)) print(tuples)
# ((1,), (2, 4, 3), (6, 5))Try it yourself:
https://trinket.io/embed/python/988e6b062f
Video tutorial on the
Code:
map()The first argument of the
Code:
map()Code:
tupleThis
Code:
tuple()Code:
listsThe result of the
Code:
map()Method 3: Simple For Loop with append() and tuple()
To convert a list of lists to a tuple of tuples, first initialize an empty “outer” list and store it in a variable.
Then iterate over all lists using a simple
Code:
forNext, append each result to the outer list variable using the
Code:
list.append()Finally, convert the list of tuples to a list tuple of tuples using the
Code:
tuple()The following example does exactly that:
Code:
lists = [[1], [2, 4, 3], [6, 5]] tmp = []
for t in lists: tmp.append(tuple(t))
tuples = tuple(tmp) print(tuples)
# ((1,), (2, 4, 3), (6, 5))Related Video Tutorial
Related Conversion Articles
- How to Convert a List of Lists to a List of Tuples
- How to Convert a List of Lists to a Pandas Dataframe
- https://blog.finxter.com/how-to-convert-...mpy-array/How to Convert a List of Lists to a NumPy Array
- How to Convert a List of Lists to a Dictionary in Python
- How to Convert a Tuple of Tuples to a List of Lists in Python
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!

