In this article, you will see how to convert a tuple to a list in Python with the help of various examples. In our previous tutorials, we have seen lots of Python programs that are very useful for you. After this article, you will not have any confusion regarding how to convert a tuple to a list in Python.
But, before further this article, let’s see a little about Python tuples and lists.
Headings of Contents
What is Python Tuple?
A tuple is a core data structure in Python that stores multiple items in an ordered way. Python tuple is ordered, immutable or unchangeable, and also it is able to store duplicate values.
Python tuple always starts and ends with a circular bracket ( )
. As you can see below example.
Example
tpl = ("Python", "Machine Learning", "Data Science", "Data Analyst", "Data Analyst")
What is Python List?
Python list is also a core data structure that stores multiple items in a single variable. Python list always starts and ends with square brackets [ ]
. Python list is ordered, changeable, and also allows duplicate items.
Example
lst = ["Python", "Machine Learning", "Data Science", "Data Analyst", "Data Analyst"]
Now, it’s time to see elegant ways to convert a tuple to a list in Python.
How to convert a tuple to a list in Python?
Here we will see three examples to convert Python tuples to lists using various examples.
Using list() function
Here we will convert the Python tuple to a list using the python built-in list() function.
Example: Python program to convert a tuple to list
# tuple
tpl = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
# convert into list
result = list(tpl)
print(result)
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Using for loop
Here we will use Python for loop to convert tuples to lists in Python.
Example: Python program to convert a tuple to list
# Using the for loop
tpl = ("Python", "Machine Learning", "Data Science", "Data Analyst", "Data Analyst")
result = []
for i in tpl:
result.append(i)
print(result)
Output
['Python', 'Machine Learning', 'Data Science', 'Data Analyst', 'Data Analyst']
Using list comprehension
list comprehension is the best and most concise way to create a list in Python.
Example: Python program to convert a tuple to list
tpl = ("Python", "Machine Learning", "Data Science", "Data Analyst")
# Using list comprehension
result = [i for i in tpl]
print(result)
Output
['Python', 'Machine Learning', 'Data Science', 'Data Analyst']
Conclusion
So, here we have seen all about how to convert a tuple to a list in Python using various ways. As a developer, you might be required to convert the python tuple to a list, Then you can follow all the above processes.
I hope this article will helpful for you. If you like this article, please share and keep visiting for further Python tutorials.
Related Articles:-
Ref:- Click Here
Thanks for reading… 🙏🙏🙏