Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Convert Tuple of Tuples to List of Lists in Python?
1
How to Convert Tuple of Tuples to List of Lists in Python?

5/5 – (1 vote)

[Image: 1f4ac.png] Question: Given a tuple of tuples such as
Code:
((1, 2), (3, 4))
. How to convert it to a list of lists such as
Code:
[[1, 2], [3, 4]]
?

If you’re in a hurry, here’s the most Pythonic way to convert a nested tuple to a nested list:

The list comprehension statement
Code:
[list(x) for x in tuples]
converts each tuple in
Code:
tuples
to a list and stores the results in a list of lists.

[Image: 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.

So keep reading!

Method 1: List Comprehension + list()

The recommended way to convert a tuple of tuples to a list of lists is using list comprehension in combination with the built-in
Code:
list()
function like so:
Code:
[list(x) for x in tuples]
.

Here’s a concrete example:

Code:
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:

https://trinket.io/embed/python/0c1caeca9b

This approach is simple and effective. List comprehension defines how to convert each tuple (
Code:
x
in the example) to a new list element. As each list element is a new list, you use the constructor
Code:
list(x)
to create a new list from the tuple
Code:
x
.



Example Three Elements per Tuple

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

Code:
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):

https://pythontutor.com/iframe-embed.htm...nces=false

Example Varying Number of Tuple Elements

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

Code:
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 tuple of tuples to a list of lists.

But are there any alternatives?

Method 2: Use Asterisk and List Comprehension

A variant of the recommended way to convert a tuple of tuples to a list of lists is using list comprehension in combination with the unpacking asterisk operator
Code:
*
like so:
Code:
[[*x] for x in tuples]
.

Here’s an example:

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

The unpacking operator
Code:
[*x]
takes all tuple elements from
Code:
x
and “unpacks” them in the outer list container
Code:
[...]
. For example, the expression
Code:
[*(5, 6, 5)]
yields the list
Code:
[5, 6, 5]
.

Let’s have a look at a completely different approach to solve this problem:

Method 3: Map Function + list()

Use the map function that applies a specified function on each element of an iterable.

[Image: 1f4a1.png]Side Note: Guido van Rossum, the creator of Python, didn’t like the
Code:
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 tuple of tuples into a list ot lists using the
Code:
map()
function:

Code:
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:

https://trinket.io/embed/python/e4c1895d61

Video tutorial on the
Code:
map()
function:



The first argument of the
Code:
map()
function is the
Code:
list
function name.

This
Code:
list()
function converts each element on the given iterable
Code:
tuples
(the second argument) into a list.

The result of the
Code:
map()
function is an iterable too, 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.

Method 4: Simple For Loop with append() and list()

To convert a tuple of tuples to a list of lists, a simple three-liner is to first initialize an empty “outer” list and store it in a variable. Then iterate over all tuples using a simple
Code:
for
loop and convert each separately to a list and append each result to the outer list variable using the
Code:
list.append()
builtin method in the loop body.

The following example does exactly that:

Code:
tuples = ((1,), (2, 3, 4), (5, 6, 7, 8)) lists = [] for t in tuples: lists.append(list(t)) print(lists) # [[1], [2, 3, 4], [5, 6, 7, 8]]

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?

[Image: 1f680.png] 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!
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: