about dictionary
Ben Finney
bignose+hates-spam at benfinney.id.au
Sun Nov 20 06:33:21 EST 2005
Shi Mu <samrobertsmith at gmail.com> wrote:
> 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:[]}
Explicit:
a = [3, 5, 8, 0]
b = {}
for key in a:
b[key] = []
Quicker:
a = [3, 5, 8, 0]
b = dict( [(k, []) for k in a] )
What behaviour do you expect when there are repeated values in the
list 'a'?
--
\ "Faith may be defined briefly as an illogical belief in the |
`\ occurrence of the improbable." -- Henry L. Mencken |
_o__) |
Ben Finney
More information about the Python-list
mailing list