Remove Duplicates from a List of Tuples of Different Length with Python

share link

by Abdul Rawoof A R dot icon Updated: Feb 25, 2023

technology logo
technology logo

Guide Kit Guide Kit  

To remove the duplicates from a list in Python, we can use the built-in function set(), and the specialty of the set() method is that it returns distinct elements. 

To remove duplicates from the list of tuples, the loop, any method, and the enumerate method can be used in Python. A list can store heterogeneous values, that is, data of any data type like integer, string, floating points, etc. We can find where the duplicates are present in the list of tuples by iterating through the tuples list, and for each tuple, we must check if it exists in the dictionary. If it does, it means that the tuple is a duplicate, and we can add it to a separate list of duplicate tuples, and if it does not, we can add it to the dictionary as a key with the value of 1. The distinct keyword is used for duplicate tuples, and to ensure that duplicates are not removed, we can use all keywords. To remove duplicates regardless of order, we need to sort the list's tuples, and then check for duplicate values to remove them and print the values; Python provides multiple methods to perform a task. 

The quick way to remove duplicates from a list in Python: 

  • List Comprehension. 
  • Using Dictionary Keys. 
  • List count() function. 
  • Using the set() built-in method. 
  • Using a temporary list and looping. 

Here is an example of how to remove duplicates from a list of tuples of different lengths in Python: 

Fig : Preview of the output that you will get on running this code from your IDE.


lst = [
[('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')],
[('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Justice'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')],
[('Lord', 'Justice', 'Smith'), ('Lady', 'Smiles'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')],
[('Lord', 'Justice', 'Smith'), ('Lady', 'Justice'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')],
[('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lady', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]
res = [[max(reversed(list(v)), key=len) for k,v in groupby(sl, lambda x: x[0])] for sl in lst]
for l in res:

[('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]
[('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]
[('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]
[('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]
[('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')]


Follow the steps carefully to get the output easily.

  1. Install python on your IDE(Any of your favorite IDE).
  2. Copy the snippet using the 'copy' and paste it in your IDE.
  3. Import 'groupby'(refer preview of the output).
  4. Run the file to generate the output.

I hope you found this useful.

I found this code snippet by searching for 'how to remove duplicates from a list of tuples of different length with python' in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in PyCharm 2021.3.
  2. The solution is tested on Python 3.9.7.

Using this solution, we are able to remove duplicates from list of tuples of different length with python with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to remove duplicates from list of tuples of different length with python.


  1. For any support on kandi solution kits, please use the chat
  2. For further learning resources, visit the Open Weaver Community learning page.