We use lists and tuples in Python if we want random access to sequential data. However, lists and tuples have a fundamental difference. Tuples in Python are immutable while we can manipulate lists in Python. In this article, we will discuss how to convert a tuple to a list in Python.
Tuple to List in Python Using For Loop
To convert a list to a tuple in Python, we can use a for loop and the append()
method. For this, we will use the following approach.
- First, we will create an empty list named
output_list
. - After creating the empty list, we will iterate through the elements of the tuple using a for loop.
- While iteration, we will append each element of the tuple to
output_list
. For this, we will invoke theappend()
method onoutput_list
and pass the elements of the tuple as input arguments. After execution of theappend()
method, the elements are added tooutput_list
.
After execution of the for loop, we will get the list created from the tuple. You can observe this in the following example.
myTuple=(11,2,44,21,15,4,23,12)
output_list=[]
for element in myTuple:
output_list.append(element)
print("The original tuple is:")
print(myTuple)
print("The output list is:")
print(output_list)
Output:
The original tuple is:
(11, 2, 44, 21, 15, 4, 23, 12)
The output list is:
[11, 2, 44, 21, 15, 4, 23, 12]
Convert Tuple to List Using The extend() Method
Instead of using the for loop and the append()
method, we can also use the extend()
method to convert a tuple into a list. The extend()
method takes an iterable object as its input argument and adds all the elements of the input object to the list. To convert a tuple to a list using the extend()
method, we will use the following steps.
- First, we will create an empty list
output_list
to store the output list. - Next, we will invoke the
extend()
method onoutput_list
. Theextend()
method takes the tuple as its input argument. After execution, it will add all the elements of the tuple to the list.
After execution of the extend()
method, we will get the output list. You can observe this in the following example.
myTuple=(11,2,44,21,15,4,23,12)
output_list=[]
output_list.extend(myTuple)
print("The original tuple is:")
print(myTuple)
print("The output list is:")
print(output_list)
Output:
The original tuple is:
(11, 2, 44, 21, 15, 4, 23, 12)
The output list is:
[11, 2, 44, 21, 15, 4, 23, 12]
Tuple to List Using List Comprehension in Pythn
List comprehension is used to create lists from existing iterable objects in Python. We can also use it to convert the tuple into a list as shown below.
myTuple=(11,2,44,21,15,4,23,12)
output_list=[element for element in myTuple]
print("The original tuple is:")
print(myTuple)
print("The output list is:")
print(output_list)
Output:
The original tuple is:
(11, 2, 44, 21, 15, 4, 23, 12)
The output list is:
[11, 2, 44, 21, 15, 4, 23, 12]
Using the List() Function in Python
Python also provides us with the list()
function to directly convert an iterable object into a list. We can pass the tuple to the list()
function to convert it into a list as shown below.
myTuple=(11,2,44,21,15,4,23,12)
output_list=list(myTuple)
print("The original tuple is:")
print(myTuple)
print("The output list is:")
print(output_list)
Output:
The original tuple is:
(11, 2, 44, 21, 15, 4, 23, 12)
The output list is:
[11, 2, 44, 21, 15, 4, 23, 12]
Conclusion
In this article, we discussed different ways to convert a tuple to a list in Python. To learn more about Python programming, you can read this article on pandas where method. You might also like this article on how to convert a string to a variable name in Python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.