I was studying dictionaries and looking for ways to avoid the worst-case O(n) time complexity for get/set/delete operations due to potential hash collisions (src), and learned that integers always hash to themselves, so collisions shouldn't be an issue if you use ints as the dictionary's keys. However, I was testing this in my terminal and this is what I saw:
>>> print hash(4), hash(3), hash(2), hash(1), hash(0), hash(-1), hash(-2), hash(-3), hash(-4)
4 3 2 1 0 -2 -2 -3 -4
>>> hash(-1) == hash(-2)
True
That's strange, both hash(-1) == -2
and hash(-2) == -2
so I tried it in a dict:
>>> d = {-3: 'a', -2:'b', -1:'c'}
>>> print d
{-1: 'c', -3: 'a', -2: 'b'}
Okay, at least the hash collision is handled properly.
Why are there two ints that have the same hash?