Calmcode - comprehensions: dict

Dict

1 2 3 4 5 6 7 8 9

Comprehensions in python are not just limited to lists. They can also be used on many data containers. Below is an example of a list-comprehension.

[c for i, c in enumerate('abceabce') if i < 5]

But there are also set comprehensions.

{c for i, c in enumerate('abceabce')}

And there are tuple comprehensions.

tuple(c for i, c in enumerate('abceabce'))

Even dictionary comprehensions!

{i: c for i, c in enumerate('abceabce') if i < 5}