How to convert a list into a tuple in Python

In this tutorial, we learn how to convert the list into a tuple in Python. As you can see List is mutable and tuple is immutable.

Here we can see how to change the list parameter in the tuple. As you can convert String Parameter and Integer Parameter List into a tuple here we can see how we can convert both.

Convert String List into a tuple in Python

# here we create the list for convert into a tuple.
Str_list=['Avinash','Sonu','Sajif','Vipul']
# print list.
Print(Str_list)
# Convert list into tuple using tuple() built-in Python.
Str_tuple=tuple(Str_list)
#print tuple.
print(Str_tuple)

output:

['Avinash','Sonu'','Sajif','Vipul']

('Avinash','Sonu','Sajif','Vipul')

Explanation: Here I take string data for creating a list and convert that string data in a tuple and print that using the print() function.

Convert Integer List into a tuple in Python

# Taking Integaer parameter List
Int_list=[12,22,32,43,67]
# Print list
print(Int_list)
# Convert Interger List into a Tuple
Int_tuple=tuple(Int_list)
# Print tuple
print(Int_tuple)

output:

[12,22,32,43,67]

(12,22,32,43,67)

Explanation: Here I take Integer Parameter for creating a list and convert that Integer data in a tuple and print that using the print() function.

Here you can see you can Convert String Parameter and Integer Parameter List into a tuple. As like this you can also convert both list objects in a tuple.

You may also learn:

Leave a Reply

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