Python multimap

Miles semanticist at gmail.com
Wed Aug 27 20:08:40 CEST 2008


brad wrote:
> There is only one '1' key in your example. I need multiple keys that are all
> '1'. I thought Python would have something built-in to handle this sort of
> thing.
>
> I need a true multimap ... without making K's value a list of stuff
> to append to.

That's what a multimap is.  If you really need the syntactic sugar,
it's simple to implement:

class multidict(dict):
  def __setitem__(self, key, value):
    try:
      self[key].append(value)
    except KeyError:
      dict.__setitem__(self, key, [value])

-Miles



More information about the Python-list mailing list