+2 votes
in Programming Languages by (11.5k 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

0 votes
by (20k points)

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'

...