Cookbook: Associating multiple values with each key in a dictionary

Steven sadams123 at optushome.com.au
Tue Jul 30 19:26:17 EDT 2002


"Dave Reed" <dreed at capital.edu> wrote in message
news:mailman.1028062367.18700.python-list at python.org...
> > From: Egbert Bouwman <egbert at bork.demon.nl>
> >
> > On Tue, Jul 30, 2002 at 01:14:14PM -0400, Michael Gilfix wrote:
> > >
> > >   I'm not really sure why you prefer the list method. Is there
something
> > > that you can't accomplish with the nested dictionaries?
> >
> > When I need
> >   { 'boys': ['mike', 'egbert'], 'girls': ['laura','lisa'] }
> > it hurts me when I have to build:
> >   { 'boys': {'mike' : 1, 'egbert' : 1}, 'girls': {'laura' : 1,'lisa' :
1} }
> >
> > That adds an extra layer of complexity when i construct it,
> > I have to remember or document that the values can be thrown away,
> > and I have now sets of keys, while I need sets of values.

...........

> The following works:
>
> { 'boys': ('mike', 'egbert'), 'girls': ('laura','lisa') }

except that you can't change a tuple (can you?), you'd have to recreate the
whole tuple each time you add or delete any values

Steven





More information about the Python-list mailing list