Python Find Duplicates in List

Python find duplicates in list | We will discuss how to find duplicate items or elements in the list. In Python, there are many methods available on the list data type that help you find duplicates elements from a given list. In this post, we are using set(), count(), list comprehension, enumerate(), slicing + in operator, and Brute Force approach.

We will take the list while declaring the variables then, the Python program will find duplicates elements from the list. Finally, the duplicates element will be displayed on the screen.

Find Duplicates in a List in Python

Using set() Function

Python provides a built-in function set(). The set() is the collection of unordered items. Each element in the set must be unique, immutable, and the sets remove the duplicate elements. Sets are mutable which means we can modify them after their creation.

# Python program to find duplicate items in list

# take list
my_list = [1, 3, 7, 1, 2, 7, 5, 3, 8, 1]

# printing original list
print('List:', my_list)

# find duplicate items using set()
seen = set()
duplicate_item = [x for x in my_list if x in seen or (seen.add(x) or False)]

# printing duplicate elements
print('Duplicate Elements:', duplicate_item)

Output:-

List: [1, 3, 7, 1, 2, 7, 5, 3, 8, 1]
Duplicate Elements: [1, 7, 3, 1]

To get each duplicate only once, you can use the set comprehension like this.

# Python program to find duplicate items in list

# take list
my_list = [1, 3, 7, 1, 2, 7, 5, 3, 8, 1]

# printing original list
print('List:', my_list)

# find duplicate items using set()
seen = set()
duplicate_item = {x for x in my_list if x in seen or (seen.add(x) or False)}

# printing duplicate elements
print('Duplicate Elements:', duplicate_item)

Output:-

List: [1, 3, 7, 1, 2, 7, 5, 3, 8, 1]
Duplicate Elements: [1, 7, 3]

Count Duplicates in List in Python

Count() is an inbuilt function in Python that returns the count of how many times a given object occurs in a list. Syntax: list_name.count(object)

# Python program to find duplicate items in list

# take list
my_list = [1, 3, 7, 1, 2, 7, 5, 3, 8, 1]

# printing original list
print('List:', my_list)

# find duplicate items using count()
duplicate_item = {x for x in my_list if my_list.count(x) > 1}

# printing duplicate elements
print('Duplicate Elements:', duplicate_item)

Output:-

List: [1, 3, 7, 1, 2, 7, 5, 3, 8, 1]
Duplicate Elements: [1, 7, 3]

Using the Brute Force approach

# Python program to find duplicate items in list

# find duplicate items using Brute Force approach
def duplicate_item(x):
    size = len(x)
    duplicate = []
    for i in range(size):
        k = i + 1
        for j in range(k, size):
            if x[i] == x[j] and x[i] not in duplicate:
                duplicate.append(x[i])
    return duplicate

# take list
my_list = [1, 3, 7, 1, 2, 7, 5, 3, 8, 1]

# printing original list
print('List:', my_list)

# printing duplicate elements
print('Duplicate Elements:', duplicate_item(my_list))

Output:-

List: [1, 3, 7, 1, 2, 7, 5, 3, 8, 1]
Duplicate Elements: [1, 3, 7]

Using index() Function

The index() method returns the position at the first occurrence of the specified value. Check for another occurrence of each encountered element. Syntax: list_name.index(element)

# Python program to find duplicate items in list

# take list
my_list = [1, 3, 7, 1, 2, 7, 5, 3, 8, 1]

# printing original list
print('List:', my_list)

# find duplicate items using index()
duplicate_item = [x for i, x in enumerate(my_list) if i != my_list.index(x)]

# printing duplicate elements
print('Duplicate Elements:', duplicate_item)

Output:-

List: [1, 3, 7, 1, 2, 7, 5, 3, 8, 1]
Duplicate Elements: [1, 7, 3, 1]

Using slicing + in operator

Alternatively, you can use slicing with the in operator to search in the already visited portion of the list.

# Python program to find duplicate items in list

# take list
my_list = [1, 3, 7, 1, 2, 7, 5, 3, 8, 1]

# printing original list
print('List:', my_list)

# find duplicate items using slicing + in operator
duplicate_item = [x for i, x in enumerate(my_list) if x in my_list[:i]]

# printing duplicate elements
print('Duplicate Elements:', duplicate_item)

Output:-

List: [1, 3, 7, 1, 2, 7, 5, 3, 8, 1]
Duplicate Elements: [1, 7, 3, 1]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *