Python Tuple to List

python-tuple-to-list-ipcisco-1

In Python Programming, to convert Python Tuple to List, we use list method. As we mentined in other lessons, we can create lists with list method. So, to create a list from a python tuple, we can also use python list method.

 

Here, to convert Python Tuple to List, we will use the tuple as the parameter of the list method like below:

 

cars = ("bmw", "audi", "volvo", "mercedes")
a = list(cars)
print(a)

Normally, cars is a python tuple. But with this code and with python function method, we convert python tuple to the python list. The output of this python code will be:

 

['bmw', 'audi', 'volvo', 'mercedes']

You can practice below!


You can also check Tuple Methods of Python


 

python-tuple-to-list-ipcisco-1

 

You can also watch the video of this lesson!

 

Let’s do this with a number tuple.

 

numbers = (1,2,3,4,5)
a = list(numbers)
print(a)

 

[1, 2, 3, 4, 5]

 

python-tuple-to-list-ipcisco-2

 


You can also check Python Tuple Unpacking


 

We use Tuple List convertion for different purposes. One of these purposes is to change the content of python tuple. To do this, firsty we change Tuple to List with the help of list method. And then, we change the members of the created list. We can add or remove members. And then, we can convert python list to tuple with tuple method.

 

Let’s give an example to understand this usage better:

 

cars = ("bmw", "audi", "volvo", "mercedes")
a = list(cars)
a.append("aston martin")
cars = tuple(a)
print(cars)

 

As you can see below,in the output of this code, you will see the new member in the python  tuple.

 

('bmw', 'audi', 'volvo', 'mercedes', 'aston martin')

 

how-to-convert-python-tuple-to-python-list

 

As we have mentined in the python tuple vs list lesson, python tuples are immutable. We can not change python tuples. For example there is no usage like python tuple append. In other words, we can not use append method with tuples to add a new tuple member. But with this trick we can do this.

 

In this lesson, we have learned how to convert Python Tuple to List. We have created a pyhon list with the help of a tuple. Then we have learned how to modify a tuple with a trich way with the help of list function and tuple fucntion. You can use this method to do this job in your following codes.

Back to: Python Programming Course > Python Tuples

Leave a Reply

Your email address will not be published. Required fields are marked *

Python Programming Course

Collapse
Expand
Latest Blog Posts