Checking for the existence of Duplicates

AndyB andrewblakeslee at gmail.com
Fri Sep 28 17:27:58 EDT 2007


I have found a lot of material on removing duplicates from a list, but I 
am trying to find the most efficient way to just check for the existence 
of duplicates in a list.  Here is the best I have come up with so far:

             CheckList = [x[ValIndex] for x in  self.__XRList[z]]
             FilteredList = filter((lambda x:x != 0),CheckList)
             if len(FilteredList) > len(sets.Set(FilteredList)): return 
False

The first statement pulls the slice out of a matrix I need to check.
The filter statement gets rid of zeroes (they don't count as duplicates).
The if statement is the actual duplicates check

This is in a program that generates random numbers to do a brute force 
solve on a sudoku-like puzzle.  Once a certain level of difficulty in 
the puzzle is reached, performance goes off a cliff because the 
duplicate checking code, although fast, is executed so many times.



More information about the Python-list mailing list