Python multimap

brad byte8bits at gmail.com
Wed Aug 27 13:52:36 EDT 2008


Mike Kent wrote:

> Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
> [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> k = {}
>>>> k['1'] = []
>>>> k['1'].append('Tom')
>>>> k['1'].append('Bob')
>>>> k['1'].append('Joe')
>>>>
>>>> k['1']
> ['Tom', 'Bob', 'Joe']

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:

k['1'] = 'Tom'
k['1'] = 'Tommy'

without Tommy overwriting Tom and without making K's value a list of 
stuff to append to. That's still just a regular map.



More information about the Python-list mailing list