+3 votes
in Programming Languages by (74.2k points)
How can I make all items a list keys of a dictionary?

E.g.

From

[11,12,13]

to

{11: 'hello', 12: 'hello', 13: 'hello'}

1 Answer

+1 vote
by (349k points)
selected by
 
Best answer

The pythonic way to convert all elements of a list to keys of a dictionary is as follows:

>>> aa=[11,12,13]
>>> bb={v:'hello' for v in aa}
>>> bb
{11: 'hello', 12: 'hello', 13: 'hello'}
>>> bb[12]
'hello'


...