Sometimes, we want to use Python dictionary comprehension.
In this article, we’ll look at how to use Python dictionary comprehension.
How to use Python dictionary comprehension?
To use Python dictionary comprehension, we can use it to map a dict’s keys and values to new values.
For instance, we write
d = {n: n**2 for n in range(5)}
to create a dictionary with key n
and value with n
squared.
And we create entries with n
from 0 to 4 with range(5)
.
Conclusion
To use Python dictionary comprehension, we can use it to map a dict’s keys and values to new values.