Python: Convert Tuple to List and Vice versa

python programs

Summary: In this tutorial, we will learn to convert a tuple into a list and vice versa in Python.

Tuple to List in Python

To convert a tuple into a list, we pass the tuple object to the list() function as a parameter.

The list() function accepts an iterable as an argument and constructs a list object based on the iterable’s elements.

#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 into a tuple, we pass the list object to the tuple() function as a parameter.

The tuple() function accepts an iterable as an argument and constructs a tuple object based on its elements.

#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)

Leave a Reply

Your email address will not be published. Required fields are marked *