Python Program to Remove Duplicate elements from a Tuple

In the previous article, we have discussed Python Program to Generate Perfect Numbers in an Interval
Tuple in Python:

A tuple is an immutable list of objects. That means the elements of a tuple cannot be modified or changed while the program is running.

The elements from any tuple can be removed by slicing the tuple.

Sometimes elements or objects in Python tuple are repeated; these repeated elements and objects are known as duplicate elements in Python tuple.

Counter() function in Python:

Python Counter is a container that keeps track of the count of each element in the container. The counter class is a subclass of the dictionary class.

The counter class is a subclass of the dictionary class. You can count the key-value pairs in an object, also known as a hash table object, using the Python Counter tool.

Examples:

Example 1:

Input:

Given Tuple = (3, 5, 1, 4, 5, 3, 1)

Output:

The given tuple after removal of duplicate elements =  (3, 5, 1, 4)

Example 2:

Input:

Given Tuple = (1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 7, 8)

Output:

The given tuple after removal of duplicate elements = (1, 2, 3, 4, 5, 6, 7, 8)

Program to Remove Duplicate elements from a Tuple

Below are the ways to remove duplicate elements from a Given Tuple.

Method #1: Using Counter() Function (Static input)

Approach:

  • Import Counter function from collections module using the import keyword.
  • Give the tuple as static input and store it in a variable.
  • Apply the Counter function on the given tuple and store it in another variable say “freq”.
  • Get all the keys from the above-obtained counter “freq” using the freq. keys() method() (contains all unique elements ).
  • Store it in another variable say “unique_values”.
  • Convert the above got “unique_values” to tuple using the tuple() function and store it in another variable.
  • Print the above-given tuple after removal of duplicate elements.
  • The Exit of the program.

Below is the implementation:

# Import Counter function from collections module using the import keyword.
from collections import Counter
# Give the tuple as static input and store it in a variable.
gvn_tupl = (3, 5, 1, 4, 5, 3, 1)
# Apply the Counter function on the given tuple and store it in another variable.
freq = Counter(gvn_tupl)
# Get all the keys from the above-obtained counter "freq" using freq. keys() method()
# (contains all unique elements ) .
# Store it in another variable say "unique_values".
unique_values = freq.keys()
# Convert the above got "unique_values" to tuple using tuple() function and
# store it in another variable.
reslt = tuple(unique_values)
# Print the above-given tuple after removal of duplicate elements.
print("The given tuple after removal of duplicate elements = ", reslt)

Output:

The given tuple after removal of duplicate elements =  (3, 5, 1, 4)

Method #2: Using Counter() Function (User input)

Approach:

  • Import Counter function from collections module using the import keyword.
  • Give the tuple as user input using tuple(),map(),input(),and split() functions and Store it in a variable.
  • Apply the Counter function on the given tuple and store it in another variable say “freq”.
  • Get all the keys from the above-obtained counter “freq” using the freq. keys() method() (contains all unique elements ).
  • Store it in another variable say “unique_values”.
  • Convert the above got “unique_values” to tuple using the tuple() function and store it in another variable.
  • Print the above-given tuple after removal of duplicate elements.
  • The Exit of the program.

Below is the implementation:

# Import Counter function from collections module using the import keyword.
from collections import Counter
#Give the tuple as user input using tuple(),map(),input(),and split() functions 
#and Store it in a variable.
gvn_tupl = tuple(map(int, input( 'Enter some random tuple Elements separated by spaces = ').split()))
# Apply the Counter function on the given tuple and store it in another variable.
freq = Counter(gvn_tupl)
# Get all the keys from the above-obtained counter "freq" using freq. keys() method()
# (contains all unique elements ) .
# Store it in another variable say "unique_values".
unique_values = freq.keys()
# Convert the above got "unique_values" to tuple using tuple() function and
# store it in another variable.
reslt = tuple(unique_values)
# Print the above-given tuple after removal of duplicate elements.
print("The given tuple after removal of duplicate elements = ", reslt)

Output:

Enter some random tuple Elements separated by spaces = 1 2 3 1 2 3 4 5 6 7 7 8
The given tuple after removal of duplicate elements = (1, 2, 3, 4, 5, 6, 7, 8)

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.