ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.