Home »
Python »
Python Programs
Python program to convert set into tuple and tuple into set
Here, we are going to learn how to convert set into tuple and tuple into set in Python programming language?
By Shivang Yadav Last updated : December 15, 2023
Problem statement
Write Python programs to convert a set into tuple and tuple into a set.
Prerequisites
To understand the solution, you must know the following Python topics:
Converting set into tuple and tuple into set
Python allows interconversion between two types of collections. This is easily done by passing the other type of collection in the creation function of the collection.
- To convert to Tuple : tuple(collection)
- To convert to Tuple : set(collection)
Tuple to set conversion
To convert a set into a tuple, use the tuple() method and pass the set as its argument.
Python program to convert set to tuple
# Python program to convert
# set to tuple in Python
# Creating and print the set
mySet = {4, 2, 6, 8, 19}
print("The set is " + str(mySet))
# converting set to tuple
myTuple = tuple(mySet)
# printing the tuple
print("The tuple is " + str(myTuple))
Output:
The set is {2, 4, 6, 8, 19}
The tuple is (2, 4, 6, 8, 19)
Set to tuple conversion
To convert a tuple into a set, use the set() method and pass the tuple as its argument.
Python program to convert tuple into set
# Python program to convert
# tuple to set in Python
# Creating and print the Tuple
myTuple = (4, 2, 6, 8, 19)
print("The tuple is " + str(myTuple))
# Converting Tuple to set
mySet = set(myTuple)
# Printing the tuple
print("The set is " + str(mySet))
Output:
The tuple is (4, 2, 6, 8, 19)
The set is {2, 4, 6, 8, 19}
Python Tuple Programs »