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.

#creating tuple
tup=(0,1,2,5)
print("Tuple: ",tup)

#coverting the tuple to a list
l = list(tup)
print("List: ",l)

Output

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.

#creating list
l = [0, 1, 2, 5]
print("List: ",l)

#converting the list into a tuple
tup = tuple(l)
print("Tuple: ",tup)

Output

List:  [0, 1, 2, 5]
Tuple:  (0, 1, 2, 5)

Comment below any suggestion you have.

Leave a Reply