about dictionary

Shi Mu samrobertsmith at gmail.com
Sun Nov 20 13:12:32 CET 2005


On 11/20/05, Peter Otten <__peter__ at web.de> wrote:
> przemek drochomirecki wrote:
>
> > Uzytkownik "Shi Mu" <samrobertsmith at gmail.com> napisal w wiadomosci
> > news:mailman.888.1132484139.18701.python-list at python.org...
> > I have have the following code:
> >>>> a=[3,5,8,0]
> >>>> b={}
> >>>>
> > How I can i assign each item in a as the key in the dictionary b
> > simultaneously?
> > that is,
> > b={3:[],5:[],8:[],0:[]}
> > Thanks!
> >
> >
> > Other solution:
> > b.fromkeys(a,[])
>
> Be warned that all keys share the same value.
>
> >>> a = [3, 5, 8, 0]
> >>> b = dict.fromkeys(a, [])
> >>> b[3].append(42)
> >>> b
> {8: [42], 0: [42], 3: [42], 5: [42]}
>
> Probably not what you want...
>
> Peter
how to do with it?



More information about the Python-list mailing list