Q:

Python program to convert set into tuple and tuple into set

belongs to collection: Python Tuple Programs

0

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)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to extract tuples having K digit el... >>
<< Python program to convert integer values in a list...