dict slice assignment with tuples?
Berthold Höllmann
se6y095 at public.uni-hamburg.de
Fri Sep 17 03:49:38 EDT 1999
wtanksle at dolphin.openprojects.net (William Tanksley) writes:
> On Wed, 15 Sep 1999 14:45:44 -0700 (PDT), Nathan Clegg wrote:
> >I appreciate python's ability to assign several values to several keys in
> >a dictionary, such as in:
>
> >a = {}
> >a['b', 'c', 'd'] = [4, 5, 6]
>
> Before you go on, I have to warn you that this may not be doing what you
> expect. It's actually not creating several keys; it's creating a single
> key, whose value is ('a','c','d').
>
> What you're expecting would be kinda nice, no doubt. IIRC, you can get it
> with:
>
> a = {}
> a.update( { 'b':4, 'c':5, 'd':6 } )
>
> Yup, that seems to work.
Hello,
Yes, it works, but think it is not what Nathan wants. I guess
>>> a = {}
>>> def x():
... return (4, 5, 6)
...
>>> map(lambda k,v,a=a:a.update({k:v}) , ('b', 'c', 'd'), x())
[None, None, None]
>>> a
{'d': 6, 'b': 4, 'c': 5}
>>>
looks much more like what is wanted.
Cheers
Berthold
--
bhoel at starship.python.net / http://starship.python.net/crew/bhoel/
It is unlawful to use this email address for unsolicited ads
(USC Title 47 Sec.227). I will assess a US$500 charge for
reviewing and deleting each unsolicited ad.
More information about the Python-list
mailing list