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?
Submitted by Shivang Yadav, on July 18, 2021
Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.
Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.
Example:
tuple = ("python", "includehelp", 43, 54.23)
Set is a collection of multiple values stored in a common name.
Example:
set = {"a", "includehelp", 43, 54.23}
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)
Program to convert set to Tuple in python
# 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)
Program to convert Tuple into Set in python
# 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 »