# Python Forum

Full Version: Hashing tuples
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I just discovered (the bad way) that:
`hash((-1,0)) == hash((-2,0))`
I rewrote the program using:
`hash(str(tuple))`
and got it to work.

Has someone a better idea?

Thank you!

(Using python 3.6.9)
I didn't know that. Hash collisions occur. Why is it a problem for you?
(Dec-03-2019, 10:36 PM)Gribouillis Wrote: [ -> ]I didn't know that. Hash collisions occur. Why is it a problem for you?

Well... I was trying to speed up an algorithm with an ad-hoc data structure, but it didn't work, also with small datasets. Lost some hours searching for a bug in the wrong places...

I didn't expect to find a collision so fast!
remow Wrote:I didn't expect to find a collision so fast!
`hash()` is meant to be fast in order to implement hash-table-like algorithms. If you want robust hash functions against collisions, use the hashlib module or cryptographic modules.
(Dec-03-2019, 10:51 PM)Gribouillis Wrote: [ -> ]`hash()` is meant to be fast in order to implement hash-table-like algorithms.

Yes, I liked the fast part and glossed over collisions
Tomorrow I'll try to remove the `str()` hack and take hash-collisions into account, hoping it won't be too slow/use too much memory with the full dataset (that I still haven't tried with the `str()` hack, but now I am convinced that it won't work). But now I need sleep.

Thank you!
This is because `hash(-2) == hash(-1)` and hash of a tuple is based on hashes of its items. So, it is expected that `hash((-2,-1,-2))` will be equal to `hash((-1,-1,-1))` etc.
Can you explain what you want to do? Instead of the `hash()` function you could perhaps rely directly on dictionaries.
(Dec-04-2019, 04:55 AM)Gribouillis Wrote: [ -> ]Can you explain what you want to do? Instead of the `hash()` function you could perhaps rely directly on dictionaries.

Thank you, I just did that.
At the end it was not worth the effort, I was reinventing the wheel...

Now I am waiting for the algorithm to finish.