In this tutorial, we'll learn how to Convert a List into a Tuple in python.

Method 1: using tuple() built-in function

In this following method, we'll use tuple() built-in function to convert a list to a tuple, to understand let's see the syntax and example.

Syntax


tuple(your_list)

Example:


#list
animals = ['cat', 'dog', 'zebra', 'shark']

#convert list to tuple
new_animals = tuple(animals)

#print type of variable
print(type(new_animals))

#print tuple
print(new_animals)

output

<class 'tuple'> 

('cat', 'dog', 'zebra', 'shark')

Method 2: using for loop

In this second method, we'll iterate over the list.

syntax


tuple(i for i in yourlist)

Example


#list
animals = ['cat', 'dog', 'zebra', 'shark']

#convert list to tupl
tuple1 = tuple(i for i in animals)


#print type of variable
print(type(tuple1))

#print tuple
print(tuple1)

output

<class 'tuple'> 

('cat', 'dog', 'zebra', 'shark')    

English today is not an art to be mastered it's just a tool to use to get a result