Why no list as dict key?

Dan Stromberg drsalists at gmail.com
Wed Apr 20 23:28:46 EDT 2022


On Wed, Apr 20, 2022 at 7:23 PM Abdur-Rahmaan Janhangeer <
arj.python at gmail.com> wrote:

> Maybe hashes should point to an object rather than being the hash of an
> object themselves.
> Maybe the speed drop is not worth it.
>

If you need mutable keys, you /might/ create a dict-like-object using what
functional languages call an association list.  But these have O(n)
lookups, not O(1) like a dict (AKA hash table).  And even then, if you have
keys a and b, a != b, and you modify b to look just like a, you could get a
bit of a mess from the two equal keys mapping to two different values.

It is possible to have a dict that allows duplicates though.  Usually
that's not what you want, but sometimes it is, EG:
https://stromberg.dnsalias.org/~strombrg/dupdict_mod/

Trees have much the same issue as a dict (hash table) in this case.  These
are O(logn) or so.

The problem is hash tables and trees don't want keys changing "out from
under them".  It causes things they're assuming are invariant, to vary.


More information about the Python-list mailing list