Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python methods to find duplicates

Tags:

python

Is there a way to find if a list contains duplicates. For example:

list1 = [1,2,3,4,5]
list2 = [1,1,2,3,4,5]

list1.*method* = False # no duplicates
list2.*method* = True # contains duplicates
like image 323
David542 Avatar asked Dec 01 '25 05:12

David542


1 Answers

If you convert the list to a set temporarily, that will eliminate the duplicates in the set. You can then compare the lengths of the list and set.

In code, it would look like this:

list1 = [...]
tmpSet = set(list1)
haveDuplicates = len(list1) != len(tmpSet)
like image 115
3Doubloons Avatar answered Dec 02 '25 18:12

3Doubloons