Problem: We are given a tuple and is asked to convert it into a list in python.
Tuple to List in Python
To convert a tuple to list we need to use list() method with given tuple as a parameter.
1 2 3 4 5 6 7 | #creating tuple tup=(0,1,2,5) print("Tuple: ",tup) #coverting the tuple to a list l = list(tup) print("List: ",l) |
Output
1 2 | Tuple: (0, 1, 2, 5) List: [0, 1, 2, 5] |
List to Tuple in Python
To convert a list to tuple we need to use tuple() method with given list as a parameter.
1 2 3 4 5 6 7 | #creating list l = [0, 1, 2, 5] print("List: ",l) #converting the list into a tuple tup = tuple(l) print("Tuple: ",tup) |
Output
1 2 | List: [0, 1, 2, 5] Tuple: (0, 1, 2, 5) |
Comment below any suggestion you have.