In this tutorial, we will look at how to convert a tuple to a set in Python with the help of some examples.
Why convert a tuple to a set?
A tuple is a built-in data structure in Python used to store an ordered collection of items. A set, on the other hand, is used to store a collection of objects but it does not preserve any order. Also, a set stores only unique items.
The following are some of the common use-cases when you want to convert a tuple to a set.
- To use the tuple as a set and perform some set operations.
- To remove duplicates from the tuple.
Note that you lose the inherent order in the tuple after conversion to a set (since sets are unordered collections).
How to convert a tuple to a set?
You can use the Python built-in set()
function to convert a tuple to a set. Pass the tuple as an argument to the function. It returns a set resulting from the elements of the tuple.
Let’s look at an example.
# create a tuple t = (2, 5, 1, 3) # create set from tuple s = set(t) # display the set and its type print(s) print(type(s))
Output:
{1, 2, 3, 5} <class 'set'>
Here we create a set from the tuple t
above. You can see that the set contains all the elements from the tuple. Also, note that the inherent order of the elements in the tuple is not preserved in the set.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
What if your tuple has duplicate elements? Let’s find out.
# create a tuple t = (2, 5, 5, 1, 3) # create set from tuple s = set(t) # display the set and its type print(s) print(type(s))
Output:
{1, 2, 3, 5} <class 'set'>
The resulting set contains only the distinct elements from the tuple.
Using sets to remove duplicate elements in a tuple or a list is a very common use case in Python.
You might also be interested in –
- Convert Tuple to a String in Python
- Convert Tuple to a String in Python
- Python – Convert Tuple to a String
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.