Python Check for Duplicates in List

Python check for duplicates in list | We will discuss how to check if a number is repeated in a list. In Python, there are many methods available on the list data type that help you check duplicates elements in a given list. In this post, we are using set(), len(), count(), list comprehension, enumerate(), slicing, OrderedDict.fromkeys(), and numpy function.

How to Check if a Number is Repeated 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 check for duplicates in list

# take list
my_list = [1, 3, 5, 1]

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

# check duplicates using set()
seen = set()
duplicate_item = {x for x in my_list if x in seen or (seen.add(x) or False)}
if duplicate_item:
    print('Yes, the list contains duplicates.')
else:
    print('No, the list does not contains duplicates.')

Output:-

List: [1, 3, 5, 1]
Yes, the list contains duplicates.

Using set() + len() Function

len() is a built-in function in Python. You can use the len() to get the length of the given string, array, list, tuple, dictionary, etc

# Python program to check for duplicates in list

# take list
my_list = [1, 3, 5]

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

# check duplicates using set() + len()
if len(set(my_list)) != len(my_list):
    print('Yes, the list contains duplicates.')
else:
    print('No, the list does not contains duplicates.')

Output:-

List: [1, 3, 5]
No, the list does not contains duplicates.

Using count() Function

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 check for duplicates in list

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

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

# check duplicates using count()
duplicate_item = {x for x in my_list if my_list.count(x) > 1}
if duplicate_item:
    print('Yes, the list contains duplicates.')
else:
    print('No, the list does not contains duplicates.')

Output:-

List: [1, 7, 8, 1, 5]
Yes, the list contains duplicates.

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 check for duplicates in list

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

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

# check duplicates using index()
duplicate_item = [x for i, x in enumerate(my_list) if i != my_list.index(x)]
if duplicate_item:
    print('Yes, the list contains duplicates.')
else:
    print('No, the list does not contains duplicates.')

Output:-

List: [1, 7, 8, 5]
No, the list does not contains duplicates.

Using slicing operator

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

# Python program to check for duplicates in list

# take list
my_list = [1, 3, 5, 1]

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

# check duplicates using slicing
duplicate_item = [x for i, x in enumerate(my_list) if x in my_list[:i]]
if duplicate_item:
    print('Yes, the list contains duplicates.')
else:
    print('No, the list does not contains duplicates.')

Output:-

List: [1, 3, 5, 1]
Yes, the list contains duplicates.

Using collections.OrderedDict.fromkeys()

This is the fastest method to achieve a particular task. This method is available from python2.7 onwards. It first removes the duplicates and returns a dictionary which has to be converted to a list. To make use of OrderedDict.fromkey() method, you have to import OrderedDict from collections.

# Python program to check for duplicates in list
from collections import OrderedDict

# take list
my_list = [1, 3, 5, 1]

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

# removed duplicates using collections.OrderedDict.fromkeys()
new_list = list(OrderedDict.fromkeys(my_list))

# check duplicates in the list
if my_list != new_list:
    print('Yes, the list contains duplicates.')
else:
    print('No, the list does not contains duplicates.')

Output:-

List: [1, 3, 5, 1]
Yes, the list contains duplicates.

Using numpy Method

The unique() method can help us remove duplicate elements from the given list. The output is converted back to a list format using tolist() method.

# Python program to check for duplicates in list
import numpy as np

# take list
my_list = [1, 3, 5]

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

# removed duplicates using numpy
new_list = np.unique(my_list).tolist()

# check duplicates in the list
if my_list != new_list:
    print('Yes, the list contains duplicates.')
else:
    print('No, the list does not contains duplicates.')

Output:-

List: [1, 3, 5]
No, the list does not contains duplicates.

Using list comprehension + enumerate() Function

Enumerate returns an object with a counter to each element in the list. For example (5,1), (1,3) etc. Here the first value is the index, and the second value is the list item.

# Python program to check for duplicates in list

# take list
my_list = [1, 3, 5, 1]

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

# removed duplicates using list comprehension + enumerate()
new_list = [i for n, i in enumerate(my_list) if i not in my_list[:n]]

# check duplicates in the list
if my_list != new_list:
    print('Yes, the list contains duplicates.')
else:
    print('No, the list does not contains duplicates.')

Output:-

List: [1, 3, 5, 1]
Yes, the list contains duplicates.

Also See:- Python Remove Duplicates from List

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or 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 *