Compare a dict to itself and remove similar keys efficiently
<p>I would like to compare elements in a dictionary to one another, and remove items according to some comparison criteria. And I'd love it to be efficient. I have a function that can do this, but it repeatedly copies the dictionay. Surely there is a superior way:</p>

<pre><code>mydict = {1:5,2:7,3:9,4:9,7:7,8:0,111:43,110:77}

def partial_duplicate_destroyer(mydict,tolerance):
    for key1 in mydict.keys():
        mydict_copy = mydict.copy()
        for key2 in mydict_copy.keys():
            if key2 - tolerance &lt; key1 &lt; key2 + tolerance and not(key1 == key2):
                del(mydict[key1])
                break
    return mydict

print partial_duplicate_destroyer(mydict,2)
print partial_duplicate_destroyer(mydict,20)
print partial_duplicate_destroyer(mydict,200)

#correct output:
# {4: 9, 8: 0, 111: 43}
# {8: 0, 111: 43}
# {111: 43}
</code></pre>
