REOVING TUPLE DUPLICATES USING SET
INTRODUCTION
We can remove duplicate elements by using python set() operator.
REMOVING DUPLICATES IN STRING TUPLE
We can remove duplicates using set() operator in Python.
# Removing duplicates using set() operator example 1
str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'Python', 'Python')
print("\nField of original tuple ->", str_tuple)
print("\nField of tuple after removing duplicates ->", set(str_tuple))
REOVING TUPLE DUPLICATES USING SET : Output
Field of original tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'Python', 'Python')
Field of tuple after removing duplicates -> {'XML', 'Java', 'C++', 'PHP', 'C', 'Python'}
In the above example we can see that the field “Python” was appeared thrice in the tuple. But after using set() operator, “Python” appeared only once. We can also see that the output of set operation is enclosed by curly brackets. This is indicates that the result is a set.
# Removing duplicates using set() operator example 2
str_tuple = ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'Python', 'Python', 'Java', 'PHP')
print("\nField of original tuple ->", str_tuple)
print("\nField of tuple after removing duplicates ->", set(str_tuple))
REOVING TUPLE DUPLICATES USING SET : Output
Field of original tuple -> ('Python', 'Java', 'C++', 'C', 'PHP', 'XML', 'Python', 'Python', 'Java', 'PHP')
Field of tuple after removing duplicates -> {'C++', 'Python', 'Java', 'C', 'PHP', 'XML'}
In the above example we have seen that in the original tuple Python was appeared thrice, Java and PHP both were appeared twice. But after using the set() operator all the duplicates are removed and each field in the tuple have distinct values.
REMOVING DUPLICATES IN INTEGER TUPLE
# Removing duplicates using set() operator example 3
int_tuple = (400, 200, 600, 100, 150, 600, 500, 600, 600)
print("\nField of original tuple ->", int_tuple)
print("\nField of tuple after removing duplicates ->", set(int_tuple))
REOVING TUPLE DUPLICATES USING SET : Output
Field of original tuple -> (400, 200, 600, 100, 150, 600, 500, 600, 600)
Field of tuple after removing duplicates -> {100, 200, 400, 500, 150, 600}
In the above example we have seen that the field 600 is appeared 4 times in the original tuple. But after using the set() operator the field 600 is appeared only once.
# Removing duplicates using set() operator example 4
int_tuple = (400, 200, 600, 100, 150, 600, 500, 600, 200, 150)
print("\nField of original tuple ->", int_tuple)
print("\nField of tuple after removing duplicates ->", set(int_tuple))
REOVING TUPLE DUPLICATES USING SET : Output
Field of original tuple -> (400, 200, 600, 100, 150, 600, 500, 600, 200, 150)
Field of tuple after removing duplicates -> {100, 200, 400, 500, 150, 600}
In the above example we have seen that the field 600 appeared thrice and both 200 and 150 were appeared twice. But after using set() operator all the fields have the distinct values.
REMOVING DUPLICATES IN MIXED DATA TYPE TUPLE
# Removing duplicates using set() operator example 5
mixed_tuple = (400, 200, 'PHP', 'XML', 'Python', 600, 5.8, 600, 5.8, 150, 'Python')
print("\nField of original tuple ->", mixed_tuple)
print("\nField of tuple after removing duplicates ->", set(mixed_tuple))
REOVING TUPLE DUPLICATES USING SET : Output
Field of original tuple -> (400, 200, 'PHP', 'XML', 'Python', 600, 5.8, 600, 5.8, 150, 'Python')
Field of tuple after removing duplicates -> {5.8, 200, 'XML', 400, 150, 600, 'Python', 'PHP'}
In the above example, we have seen that the original tuple is containing mixed data typed fields. Out of that the fields ‘Python‘, 600, 5.8 are appeared twice. But after using set() operator all the duplicates are removed.
Now we will check the set() operator in nested tuple.
# Removing duplicates using set() operator example 6
mixed_tuple = (400, 200, 'PHP', 'XML', 'Python', (1, 2, 3), 600, 5.8, 600, 5.8, 150, 'Python', (1, 2, 3))
print("\nField of original list ->", mixed_tuple)
print("\nField of tuple after removing duplicates ->", set(mixed_tuple))
REOVING TUPLE DUPLICATES USING SET : Output
Field of original list -> (400, 200, 'PHP', 'XML', 'Python', (1, 2, 3), 600, 5.8, 600, 5.8, 150, 'Python', (1, 2, 3))
Field of tuple after removing duplicates -> {'Python', 5.8, 'PHP', 200, (1, 2, 3), 'XML', 400, 150, 600}
So, We can see that for nested tuple set() function is working fine.
If we include a list item in the outer tuple the python will raise error.
# Removing duplicates using set() operator example 7
mixed_tuple = (400, 200, 'PHP', 'XML', 'Python', (1, 2, 3), 600, 5.8, 600, 5.8, 150, 'Python', (1, 2, 3), [5, 8, 10])
print("\nField of original list ->", mixed_tuple)
print("\nField of tuple after removing duplicates ->", set(mixed_tuple))
REOVING TUPLE DUPLICATES USING SET : Output
Field of original list -> (400, 200, 'PHP', 'XML', 'Python', (1, 2, 3), 600, 5.8, 600, 5.8, 150, 'Python', (1, 2, 3), [5, 8, 10])
Traceback (most recent call last):
File "d:\PYTHON\PROGRAM\tempCodeRunnerFile.py", line 6, in <module>
print("\nField of tuple after removing duplicates ->", set(mixed_tuple))
^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'list'
So, we can see that after including list item set() function has raised the error.

RELATED TOPICS:
- INTRODUCTION TO PYTHON TUPLE
- ACCESSING NUMBER TUPLE IN PYTHON
- ACCESSING STRING TUPLE IN PYTHON
- ACCESSING MIXED TUPLE IN PYTHON
- ACCESSING NESTED TUPLE IN PYTHON
- PYTHON TOUPLE SLICING OPERATION
- STEP SIZE IN PYTHON TUPLE SLICING
- IMMUTABILITY OF TUPLE IN PYTHON
- ZIP FUNCTION IN PYTHON TUPLE
- PYTHON TUPLE SORTING OPERATIONS
- PYTHON TUPLE BUILT-IN FUNCTIONS
- PYTHON TUPLE SHALLOW COPY
- PYTHON TUPLE DEEP COPY