Convert Tuple to List
Python set to tuple
- Strings and tuples are immutable. It means you defined it ,you can't modify it again.
- To convert a tuple into list in Python, call
list()
builtin function and pass the tuple as argument to the function. -
list()
returns a new list generated from the items of the given tuple.
How to convert tuple to list in python
You can convert your tuple into a list and work with it so that you can take advantage of the list commands and then convert it back Note : Don't use tuple, list or other special names as a variable name.
Python Tuple to List
In the following example, we initialize a tuple with all values and convert it to a list using list(sequence).
tup = (2,4,5,'Loves Python',10,'frontEndDeveloper') li = list(tup) print(type(tup)) print(tup) print(type(li)) print(li)
The Above Code Outputs
<class 'tuple'> (2, 4, 5, 'Loves Python', 10, 'frontEndDeveloper') <class 'list'> [2, 4, 5, 'Loves Python', 10, 'frontEndDeveloper']
Python List to Tuple
Here's the Vice Versa. Python tuple from list
li = [2, 4, 5, 'Loves Python', 10, 'frontEndDeveloper'] tup=tuple(li) print(type(li)) print(li) print(type(tup)) print(tup)
The Above Code Outputs
<class 'list'> [2, 4, 5, 'Loves Python', 10, 'frontEndDeveloper'] <class 'tuple'> (2, 4, 5, 'Loves Python', 10, 'frontEndDeveloper')