How to check for duplicates in a list and remove them in Python

A list is a collection of data which can be the same as each other.

There are many methods to remove the duplicates.

Table of Contents

Use set()

The set() method creates a set in Python, which doesn’t accept duplicated entries. Our approach is to use set() to convert a list to a set, then compare the length of original list with the new set.

numbers = [0, 1, 2, 3, 1, 3, 5, 9]
numbers_set = set(numbers)
if len(numbers) == len(numbers_set):
	print('No duplicate')
else:
	print('There are ' + str(len(numbers) - len(numbers_set)) + ' duplicates')

Use any()

The any() function takes an iterable (list, string, dictionary etc.) and returns True if any element of the iterable is True. If not, it returns False.

numbers = [7, 2, 5, 2, 1, 5, 5, 8]
dup = any(numbers.count(element) > 1 for element in numbers)

Leave a Comment

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

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close