0
my_dict = {}
my_dict["qwerty"]  = "some_value"
my_dict[114378642] = "some_other_value"

The above code contains a python dictionary containing two keys, where the first key is of type string and the second key is of type integer. Though both keys are of different types it produces the same hash i.e,

hash("qwerty") = 114378642
hash(114378642) = 114378642

and hence,

hash("qwerty") == hash(114378642)    #True

Couldn't get a proper answer until now,

  • Firstly, I was under an impression that "only two similar objects produce the same hash".

  • Secondly, how a python dictionary performs collision recovery in the above case?

  • Finally, what is the initial capacity and of a python dictionary in the first line of code?

lenik
  • 23,228
  • 4
  • 34
  • 43
Safdar Mirza
  • 89
  • 3
  • 11
  • 1
    "I was under an impression that only two similar objects produce the same hash" — You were under a mistaken impression. All hash-based containers have to be able to cope with hash collisions. – khelwood Jul 06 '18 at 08:07
  • @khelwood - Correct, those hash containers which I generally would call buckets need to perform collision recovery using double hashing or one of the probing techniques. All probing algorithms require the container/bucket capacity. Can you hellp me in know what the initial capacity of a python dict is? – Safdar Mirza Jul 06 '18 at 08:11

1 Answers1

0

hash() produces the same result as the int value you input, no surprise here.

>>> for i in range(10) : print i, hash(i)
... 
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

Dictionaries use a different hashing to store their values.

lenik
  • 23,228
  • 4
  • 34
  • 43