Are dictionaries the same as hashtables?

Diez B. Roggisch deets at nospam.web.de
Tue Aug 26 11:36:01 CEST 2008


Martin Marcher wrote:

> On 2008-08-26 00:32:20, cnb wrote:
>> Are dictionaries the same as hashtables?
> 
> Yes, but there is nothing in there that does sane collision handling
> like making a list instead of simply overwriting.

The term "collision" is rather well defined when talking about associative
arrays using hashing (which python's dicts are): it means that two distinct
keys produce the same hash-value, leading to a bucket collision. And of
course python deals with that sanely, and creates a list of objects inside
the bucket which is traversed and comparison is used to determine which
actual value to retrieve.

Python does not have a "one key maps to a list of values"-semantics - which
I consider the sane choice...

However, you can have that using the defaultdict for example:

listdict = defaultdict(list)

listdict[key].append(value)

Diez



More information about the Python-list mailing list