
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to convert Set into Tuple and Tuple into Set
When it is required to convert a set structure into a tuple, and a tuple into a set, the ‘tuple’ and ‘set’ methods can be used.
Below is a demonstration of the same −
Example
my_set = {'ab', 'cd', 'ef', 'g', 'h', 's', 'v'} print("The type is : ") print(type(my_set), " ", my_set) print("Converting a set into a tuple") my_tuple = tuple(my_set) print("The type is : ") print(type(my_tuple), " ", my_tuple) my_tuple = ('ab', 'cd', 'ef', 'g', 'h', 's', 'v') print("The tuple is:") print(my_tuple) print(type(my_tuple), " ", my_tuple) print("Converting tuple to set") my_set = set(my_tuple) print(type(my_set), " ", my_set)
Output
The type is : <class 'set'> {'ef', 'g', 'h', 's', 'ab', 'v', 'cd'} Converting a set into a tuple The type is : <class 'tuple'> ('ef', 'g', 'h', 's', 'ab', 'v', 'cd') The tuple is: ('ab', 'cd', 'ef', 'g', 'h', 's', 'v') <class 'tuple'> ('ab', 'cd', 'ef', 'g', 'h', 's', 'v') Converting tuple to set <class 'set'> {'ef', 'g', 'h', 's', 'ab', 'v', 'cd'}
Explanation
A set is defined and is displayed on the console.
The type of this data structure is determined using the ‘type’ method.
It is converted to a tuple using the ‘tuple’ method.
The type of this type is determined using the ‘type’ method.
Now to convert this tuple back to set, the ‘set’ method is used.
This type is determined and is displayed as the output on the console.
- Related Articles
- How to convert JSON data into a Python tuple?
- How can I convert Python strings into tuple?
- How to convert a list into a tuple in Python?
- How to convert python tuple into a two-dimensional table?
- How I can convert a Python Tuple into Dictionary?
- Convert a list into tuple of lists in Python
- Swift Program to Convert Set into Array
- Python - Convert a set into dictionary
- Python program to convert tuple into list by adding the given string after every element
- Convert set into a list in Python
- How can I append a tuple into another tuple in Python?
- Java Program to convert a set into an Array
- How to convert a tuple into an array in C#?
- Split tuple into groups of n in Python
- Python - Split list into all possible tuple pairs

Advertisements