Remove duplicate tuples from list of tuples in Python

avatar

Borislav Hadzhiev

Last updated: Jun 30, 2022

banner

Check out my new book

Remove duplicate tuples from list of tuples in Python #

To remove duplicate tuples from a list of tuples:

  1. Declare a new variable and set it to an empty list.
  2. Iterate over the list of tuples and check if each tuple is not in the new list.
  3. If the condition is met, add the tuple to the new list.
main.py
list_of_tuples = [('a', 1), ('b', 2), ('a', 1), ('b', 2), ('c', 3)] new_list = [] for tup in list_of_tuples: if tup not in new_list: new_list.append(tup) # 👇️ [('a', 1), ('b', 2), ('c', 3)] print(new_list)

The first step is to iterate over the list of tuples.

On each iteration, we check if the current tuple is not present in the new list.

The in operator tests for membership. For example, x in l evaluates to True if x is a member of l, otherwise it evaluates to False.

x not in l returns the negation of x in l.

If the condition is met, we use the list.append() method to add the tuple to the new list.

The list.append() method adds an item to the end of the list.

The new list doesn't contain any duplicate tuples.

Alternatively, you can use the set() class.

To remove duplicate tuples from a list of tuples:

  1. Use the set() class to convert the list to a set of tuples.
  2. Any duplicate tuples will automatically get removed after the conversion.
  3. Use the list() class to convert the set back to a list.
main.py
list_of_tuples = [('a', 1), ('b', 2), ('a', 1), ('b', 2), ('c', 3)] result_2 = list(set(list_of_tuples)) print(result_2) # 👉️ [('a', 1), ('b', 2), ('c', 3)]

Set objects are an unordered collection of unique elements, so when we convert the list of tuples to a set, all duplicates are automatically removed.

One thing to note though - set objects are unordered, so you don't have a guarantee that the order of the items in the list is going to be preserved.
main.py
list_of_tuples = [('a', 1), ('b', 2), ('a', 1), ('b', 2), ('c', 3)] result_2 = list(set(list_of_tuples)) print(result_2) # 👉️ [('a', 1), ('c', 3), ('b', 2)]

If you need to be sure that the order of the tuples in the list is going to be preserved, use the for loop solution.

The set() class takes an iterable optional argument and returns a new set object with elements taken from the iterable.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.