Removing duplicates from tuple in Python


When it is required to remove duplicates from a tuple, the list comprehension is used.

A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).

A list of tuple basically contains tuples enclosed in a list.

The list comprehension is a shorthand to iterate through the list and perform operations on it.

Below is a demonstration of the same −

Example

Live Demo

my_list_1 = [(11, 14), (0, 78), (33, 11), (0, 78)]

print("The list of tuple is : ")
print(my_list_1)

my_unique_list = list(set([i for i in my_list_1]))

print("The list of tuples after removing duplicates is :")
print(my_unique_list)

Output

The list of tuple is :
[(11, 14), (0, 78), (33, 11), (0, 78)]
The list of tuples after removing duplicates is :
[(33, 11), (11, 14), (0, 78)]

Explanation

  • A list of tuple is defined and is displayed on the console.
  • The list is iterated through, and is converted to set.
  • This way, only unique elements are stored.
  • This is again converted to a list.
  • This is assigned to a value.
  • It is displayed on the console.

Updated on: 12-Mar-2021

666 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements