Skip to content
Related Articles

Related Articles

Python program to convert Set into Tuple and Tuple into Set

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 05 Sep, 2022
View Discussion
Improve Article
Save Article

Let’s see how to convert the set into tuple and tuple into the set. For performing the task we are use some methods like tuple(), set(), type().

  • tuple(): tuple method is used to convert into a tuple. This method accepts other type values as an argument and returns a tuple type value.
  • set(): set method is to convert other type values to set this method is also accepted other type values as an argument and return a set type value.
  • type(): type method helps the programmer to check the data type of value. This method accepts a value as an argument and it returns type of the value.

Example:

Input: {'a', 'b', 'c', 'd', 'e'}
Output: ('a', 'c', 'b', 'e', 'd')
Explanation: converting Set to tuple

Input: ('x', 'y', 'z')
Output: {'z', 'x', 'y'}
Explanation: Converting tuple to set

Example 1: convert set into tuple.

Python




#   program to convert set to tuple
# create set
s = {'a', 'b', 'c', 'd', 'e'}
 
# print set
print(type(s), " ", s)
 
# call tuple() method
# this method convert set to tuple
t = tuple(s)
 
# print tuple
print(type(t), " ", t)


Output:

<class 'set'>   {'a', 'c', 'b', 'e', 'd'}
<class 'tuple'>   ('a', 'c', 'b', 'e', 'd')

Method2: Using list comprehension

Python3




s = {'a', 'b', 'c', 'd', 'e'}
x=[i for i in s]
print(tuple(x))


Output

('b', 'e', 'c', 'a', 'd')

Method3: Using enumerate function

Python3




s = {'a', 'b', 'c', 'd', 'e'}
x=[i for a,i in enumerate(s)]
print(tuple(x))


Output

('e', 'b', 'a', 'c', 'd')

Example 2: tuple into the set.

Python




#program to convert tuple into set
 
# create tuple
t = ('x', 'y', 'z')
 
# print tuple
print(type(t), "  ", t)
 
# call set() method
s = set(t)
 
# print set
print(type(s), "  ", s)


Output:

<class 'tuple'>    ('x', 'y', 'z')
<class 'set'>    {'z', 'x', 'y'}

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!