In this tutorial, we will look at how to remove duplicates from a tuple in Python with the help of some examples.
How to remove duplicate elements from a tuple?
Tuples are immutable in Python. That is, they cannot be modified after creation. You can, however, use a set to create a new tuple with the duplicates removed from the original tuple.
Let’s look at some examples.
Remove duplicates from a tuple using a set
You can use a set to remove duplicates from a tuple in Python. First, create a set of unique elements from the tuple using the set()
function and then use the tuple()
function to create a new tuple from the resulting set.
# create a tuple t = (1, 2, 3, 3, 4) # remove duplicates t = tuple(set(t)) print(t)
Output:
(1, 2, 3, 4)
You can see that the tuple now has only unique elements.
Note that we are not modifying the original tuple (tuples cannot be modified). Instead, we are creating a new tuple with only the unique elements and then assigning it to the variable storing the original tuple.
Using Iteration
Alternatively, you can also use iteration to remove duplicates from a tuple. Again, here we are creating a new tuple with distinct elements as tuples cannot be modified. The following are the steps.
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.
- Create an empty result list (
result_ls
) to store only the unique elements. - Iterate through each element in the original tuple.
- For each element, check if its present in our result list, if it’s not present, add it to our result list.
- Use the
tuple()
function to convert the result list to a tuple.
Let’s look at an example.
# create a tuple t = (1, 2, 3, 3, 4) # remove duplicates result_ls = [] for item in t: if item not in result_ls: result_ls.append(item) t = tuple(result_ls) print(t)
Output:
(1, 2, 3, 4)
We get the same result as above. The resulting tuple does not have any duplicates.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.