Putting two keys with the same hash into a dict
dictionary, python, python-2.x
Solution
As the accepted answer mentioned by @CoryKramer states, equality of hashes does not imply equality of objects. Python dictionaries can contain any number of elements with equal hashes as long as the objects themselves are not equal.
The short answer to your question is probably that the implementation of the `complex` type is a bit incomplete in the Python library as of 2.7. As @wim points out, comparing `int` and `complex` using `==` works fine, but comparing `Decimal` and `complex` does not. Since comparing `one_decimal == one_complex` will always return `False` because of their types, they can both live in the same dictionary in Python 2.7.
This issue has been fixed in Python 3. I am experimenting in 3.5, where `one_decimal` and `one_complex` are equal. After running the same snippet, the dictionary contains the value for `one_complex` under the key `one_decimal`, as expected (first key, last value).
TL;DR
It's a bug in Py2.7's `complex` type. Fixed in Py3.
Problem
``` >>> one_decimal = Decimal('1') >>> one_complex = complex(1,0) >>> d = {one_decimal: '1D', one_complex: '1C'} >>> len(d) 2 >>> map(hash, d) [1, 1] ``` Above, I create a dict with a hash collision, and two slots occupied. ``` >>> d[1] '1D' >>> d[1+0j] '1C' ``` How is that getitem handled for the integer `1`? And how does the indexing manage to resolve the correct value for a complex literal indexing? Python 2.7.12 / Linux.