If you’re short on time, here’s my quick take on how to convert a Python tuple to a Python list?
The most Pythonic way to convert a tuple to a list in Python is to call the built-in list()
function and pass the tuple as a function argument to the function. The function list()
takes any sequence type such as a tuple and converts them to lists. The return value of the function is the new list generated.
t = (1, 2, 3) l = list(t) print(l) # [1, 2, 3]
There are some details to this approach—and alternative ways as well. Feel free to keep reading, watch the article video, and improve your Python skills in the process! 🙂
Let’s get started!
Problem Formulation + Examples
Problem: Given a Python tuple with n
elements. How to convert it to a list with the same n
elements?
Examples:
- Convert tuple
(1, 2, 3, 4, 5)
to list[1, 2, 3, 4, 5]
. - Convert tuple
('Alice', 'Bob', 'Ann')
to list['Alice', 'Bob', 'Ann']
. - Convert tuple
(1,)
to list[1]
.
Note Tuple: Tuples are similar to lists—with the difference that you cannot change the tuple values (tuples are immutable) and you use parentheses (...)
rather than square brackets [...]
.
Solution Overview
This article shows five solutions to this problem of converting a tuple t
to a new list:
list(t)
for el in t: new_list.append(el)
[*t]
[x for x in t]
Method 1: list()
Solution: Use the built-in Python list()
function to convert a list into a tuple. You don’t need to import any external library.
Code: The following code converts the three given tuples into lists.
tuple_1 = (1, 2, 3, 4, 5) print(list(tuple_1)) # [1, 2, 3, 4, 5] tuple_2 = ('Alice', 'Bob', 'Ann') print(list(tuple_2)) # ['Alice', 'Bob', 'Ann'] tuple_3 = (1,) print(list(tuple_3)) # [1]
Explanation: You can see that converting a tuple with one element leads to a list with one element. The list()
function is the easiest way to convert a tuple into a list. Note that the values in the tuple are not copied—only a new reference to the same element is created:
The graphic also shows how to convert a tuple back to a list by using the tuple()
function (that’s also a Python built-in function). Thus, calling list(tuple(lst))
on a list lst
will result in a new list with the same elements.
Related articles:
Try to execute this code with the interactive Python tutor:
Method 2: Loop Iteration + append()
To convert a tuple to a list, start with an empty list and fill in one tuple element at a time using the append()
method and pass the tuple element into the function within a Python loop body iterating over all tuple elements.
Here’s the code to do this:
def tuple_to_list(t): new_list = [] for element in t: new_list.append(element) return new_list tuple_1 = (1, 2, 3, 4, 5) print(tuple_to_list(tuple_1)) # [1, 2, 3, 4, 5] tuple_2 = ('Alice', 'Bob', 'Ann') print(tuple_to_list(tuple_2)) # ['Alice', 'Bob', 'Ann'] tuple_3 = (1,) print(tuple_to_list(tuple_3)) # [1]
You can learn more about the append()
method in my detailed guide and the accompanying video:
Method 3: Unpacking Asterisk Operator
To convert a tuple to a list, you can use the unpacking asterisk operator * from Python version 3.5 onwards. For example, the expression [*t]
creates a new list with the square bracket notation and unpacks all elements of the tuple t
into the list.
Here’s the code to accomplish this:
def tuple_to_list(t): return [*t] tuple_1 = (1, 2, 3, 4, 5) print(tuple_to_list(tuple_1)) # [1, 2, 3, 4, 5] tuple_2 = ('Alice', 'Bob', 'Ann') print(tuple_to_list(tuple_2)) # ['Alice', 'Bob', 'Ann'] tuple_3 = (1,) print(tuple_to_list(tuple_3)) # [1]
The expression [*t]
is the most concise one-liner to convert a tuple t
to a list!
👉 Recommended: Python Unpacking [Ultimate Guide]
Method 4: List Comprehension
The list comprehension statement [x for x in t]
converts a Python tuple t
into a new list by iterating over all elements x
in the tuple t
and placing them unchanged in the list using the square bracket notation []
.
Here’s the code:
def tuple_to_list(t): return [x for x in t] tuple_1 = (1, 2, 3, 4, 5) print(tuple_to_list(tuple_1)) # [1, 2, 3, 4, 5] tuple_2 = ('Alice', 'Bob', 'Ann') print(tuple_to_list(tuple_2)) # ['Alice', 'Bob', 'Ann'] tuple_3 = (1,) print(tuple_to_list(tuple_3)) # [1]
If you need a quick refresher on list comprehension, feel free to check out my detailed guide on the Finxter blog.
Method 5: Convert Tuple of Tuples to List of Lists
If you have a tuple of tuples such as ((1, 2, 3), (4, 5, 6))
you can convert it to a list of lists such as [[1, 2, 3], [4, 5, 6]]
by combining list comprehension with the list()
function like so:
[list(x) for x in t]
Here’s a simple code example:
def tuple_to_list(t): return [list(x) for x in t] tuple_1 = ((1, 2, 3), (4, 5, 6, 7, 8, 9), ('Alice', 'Bob', 'Carl')) print(tuple_to_list(tuple_1)) # [[1, 2, 3], [4, 5, 6, 7, 8, 9], ['Alice', 'Bob', 'Carl']]
👉 Recommended: How to Convert Tuple of Tuples to List of Lists in Python?
Summary and My Recommendation
The most Pythonic way to convert a tuple to a list is using the built-in list()
function passing the tuple into it. The return value is a list with references (not copies!) to the original elements in the tuple.
my_tuple = (1, 2, 3, 4, 5) my_list = list(my_tuple) print(my_list)
In this example, we create a tuple called my_tuple
with five values. We then use the list()
function to convert the tuple to a list and assign the result to a new variable called my_list
. Finally, we print the list to the console using the print()
function.
The output of this code will be:
[1, 2, 3, 4, 5]
As you can see, the tuple has been converted to a list with the same values.
Programmer Humor – Blockchain
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.