Python multimap
Matt Nordhoff
mnordhoff at mattnordhoff.com
Wed Aug 27 10:08:16 EDT 2008
brad wrote:
> Recently had a need to us a multimap container in C++. I now need to
> write equivalent Python code. How does Python handle this?
>
> k['1'] = 'Tom'
> k['1'] = 'Bob'
> k['1'] = 'Joe'
> ...
>
> Same key, but different values. No overwrites either.... They all must
> be inserted into the container
>
> Thanks,
> Brad
I don't know if this is exactly equivalent, but what about using a
defaultdict like this?
>>> from collections import defaultdict
>>> k = defaultdict(list)
>>> k['1'].append('Tom')
>>> k['1'].append('Bob')
>>> k['1'].append('Joe')
>>> k['1']
['Tom', 'Bob', 'Joe']
--
More information about the Python-list
mailing list