From the course: Advanced Python: Classes and Functions

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Dictionary comprehensions

Dictionary comprehensions

- [Instructor] Now, let's take a look at how to use comprehensions when working with dictionaries. And in this example, I'll again use a list of temperatures. Just this time it's going to be the Celsius ones. So let's open up dictcomp start. And you can see I have my list of Celsius temperature values. So let's suppose I wanted to build a dictionary that mapped each Celsius temperature value with its corresponding value in the Fahrenheit scale. So to do that, what I would do is something like this. I'd write something like temp dict is equal to and I make two curly braces. So what I need to do in this case is create both a key and a value from each temperature in the Celsius list. And remember, we want to be able to look up each Fahrenheit value when given a Celsius value. So that means that each Celsius value needs to be the key. Okay? So I would write T colon and then that's going to be T times nine fifths plus 32 for T…

Contents