Convert Tuple to List
Convert Tuple to List in Python
Sometimes, you may be required to convert a Python Tuple into a List. It could be because of the differences between a List and Tuple.
In this tutorial, we shall learn some of the many ways to convert a tuple into list. Following are the ways for which we shall look into the examples.
- Use list() builtin function.
- Unpack Tuple inside parenthesis.
Examples
1. Convert tuple into list using list() constructor
list() is a builtin function that can take any iterable as argument and return a list object. Tuple is an iterable and we can pass it as an argument to list() function.
In the following example, we take a tuple and convert it into list using list()
constructor.
Python Program
tuple_1 = ('a', 'e', 'i', 'o', 'u')
#convert tuple into list
list_1 = list(tuple_1)
print(list_1)
print(type(list_1))
Output
['a', 'e', 'i', 'o', 'u']
<class 'list'>
You may observe in the output that we have printed the type of the list_1
object, which is <class 'list'>
.
2. Convert tuple into list by unpack tuple inside square brackets
We can also unpack the items of Tuple inside square brackets, to create a tuple.
In the following program, we have a tuple, and we shall unpack the elements of this tuple inside square brackets.
Python Program
tuple_1 = ('a', 'e', 'i', 'o', 'u')
#convert tuple into list
list_1 = [*tuple_1,]
print(list_1)
print(type(list_1))
Output
['a', 'e', 'i', 'o', 'u']
<class 'list'>
Summary
In this tutorial of Python Examples, we learned how to convert a Python Tuple into List in different ways, with the help of well detailed example programs.