Python - Find duplicates in a list

By xngo on April 2, 2020

In Python, to find all duplicate elements of a list, we have to first understand the mathematical concept of a set. A set contains only a list of distinct elements. Python provides the same concept with the same name, set.

In the example below, we will exploit the property of a set. We will read through a list of numbers and put each number into a set called seen. Since seen is a set, any duplicate numbers that we are trying to add are ignored. As result, seen will only hold a list of unique numbers. However, the numbers inside a set are stored in order. To keep the ordering and still get a list of unique numbers, we simply introduce another variable called unique. Numbers are added in unique variable at time that we add numbers in the seen variable.

my_list = [ 1, 5, 2, 4, 2, 5 ]
 
seen = set()
unique = []
duplicate = []
for x in my_list:
    if x not in seen:
        unique.append(x)
        seen.add(x)
    else:
        duplicate.append(x)
 
print(duplicate, " are duplicates.")
print(unique, " are unique.")

Output

[2, 5]  are duplicates.
[1, 5, 2, 4]  are unique.

Github

  • https://github.com/xuanngo2001/python-examples/blob/master/list/find-duplicated-in-list.py

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.