get most common number in a list with tolerance

Astan Chee astan.chee at
Fri Feb 20 03:50:08 EST 2009

I have a list that has a bunch of numbers in it and I want to get the 
most common number that appears in the list. This is trivial because i 
can do a max on each unique number. What I want to do is to have a 
tolerance to say that each number is not quite unique and if the 
difference from other numbers is small, than it can be counted together. 
This is what I have to get the common number (taken from the internet 

l = [10,30,20,20,11,12]
d = {}
tolerance = 5
for elm in l:
    d[elm] = d.get(elm, 0) + 1
counts = [(j,i) for i,j in d.items()]

This of course returns a list where each of them is unique

[(1, 12), (1, 10), (1, 11), (2, 20), (1, 30)]

but I am expecting a list that looks like this:

[(3, 10), (2, 20), (1, 30)]

What do I need to add?
Thanks for any help.

More information about the Python-list mailing list