1
00:00:00,600 --> 00:00:04,830
All right. So now we're on to the next dictionary comprehension exercise,

2
00:00:04,860 --> 00:00:08,370
which is day 26.5 dictionary comprehension two.

3
00:00:09,060 --> 00:00:09,960
And in this challenge,

4
00:00:09,990 --> 00:00:14,990
you are going to be converting a dictionary of temperatures in Celsius to their

5
00:00:16,020 --> 00:00:18,870
corresponding temperature in Fahrenheit.

6
00:00:19,320 --> 00:00:23,430
So this is how you convert a Celsius temperature to Fahrenheit.

7
00:00:23,730 --> 00:00:28,730
So the temperature multiplied by nine divided by five and then plus 32.

8
00:00:29,400 --> 00:00:33,330
So you saw this in a previous lesson as well. So pause the video,

9
00:00:33,390 --> 00:00:36,090
have a read of the instructions, take a look at the hints

10
00:00:36,090 --> 00:00:38,940
if you get stuck and see if you can complete this challenge.

11
00:00:42,200 --> 00:00:43,130
So in this case, 

12
00:00:43,190 --> 00:00:48,190
we have a slightly different exercise from before because we're now going to be

13
00:00:48,590 --> 00:00:51,710
working with a dictionary to create a new dictionary.

14
00:00:52,280 --> 00:00:56,660
Let's write our code here and let's create our new variable

15
00:00:56,660 --> 00:01:00,170
which needs to be called weather_f. And this

16
00:01:00,170 --> 00:01:02,390
weather_f is going to be a new dictionary.

17
00:01:03,110 --> 00:01:06,050
Let's put in our keyword method to

18
00:01:06,080 --> 00:01:08,480
create a new dictionary from a dictionary.

19
00:01:08,630 --> 00:01:13,630
So new_key:new_value for,

20
00:01:14,360 --> 00:01:16,550
and then it's parentheses key,

21
00:01:16,880 --> 00:01:20,150
value. So, be really careful here.

22
00:01:20,210 --> 00:01:24,470
I've seen a lot of students just use the colon in this case,

23
00:01:24,470 --> 00:01:28,160
but it's actually a tuple. So it should be key, value.

24
00:01:28,580 --> 00:01:32,630
And then we specify the dictionary that we want to loop through.

25
00:01:33,620 --> 00:01:38,090
And then we call .items on it. And remember, this is a method,

26
00:01:38,090 --> 00:01:39,980
so it needs the parentheses at the end.

27
00:01:40,640 --> 00:01:44,450
So now let's replace the relevant parts of this line.

28
00:01:44,780 --> 00:01:46,970
So our dictionary is going to be this one,

29
00:01:47,030 --> 00:01:51,920
our weather_c. That's the dictionary we're looping through.

30
00:01:52,010 --> 00:01:55,160
And we're gonna get a hold of all of the items in that dictionary

31
00:01:55,550 --> 00:02:00,170
and then get each of the keys which I'll call day, and value

32
00:02:00,170 --> 00:02:04,820
which I'll call temp_c because it will be the temperature in Celsius.

33
00:02:05,420 --> 00:02:10,420
So now that I'm looping through each of the days and the temperatures in our

34
00:02:11,300 --> 00:02:15,320
weather dictionary, then we're going to create our new key and new value.

35
00:02:15,770 --> 00:02:18,920
So the new key is actually still going to be the same day.

36
00:02:18,950 --> 00:02:23,060
It's going to be unchanged because we want this as our output

37
00:02:23,180 --> 00:02:25,100
and it looks pretty much the same as this one,

38
00:02:25,460 --> 00:02:27,920
other than the fact that we've converted the value.

39
00:02:28,490 --> 00:02:32,540
So the new value though is going to be calculated using this formula.

40
00:02:32,720 --> 00:02:37,720
So we're going to take our temp_c and then we're going to multiply it by nine

41
00:02:38,300 --> 00:02:43,270
divided by five. And then we're going to add 32. If you want,

42
00:02:43,310 --> 00:02:46,970
you can add the parentheses around this line,

43
00:02:47,300 --> 00:02:50,480
but because we're doing math and we're going from left to right,

44
00:02:50,570 --> 00:02:55,280
and we're first doing multiply divide, then we do add and subtract,

45
00:02:55,310 --> 00:02:57,860
then it's actually completely optional in this case.

46
00:02:58,070 --> 00:03:02,320
But we'll just include it in there for completion sake. So now when I hit run,

47
00:03:03,190 --> 00:03:03,870
you'll see that

48
00:03:03,870 --> 00:03:08,430
I've got a dictionary that's being generated with each of the days in the week

49
00:03:08,490 --> 00:03:09,870
from this dictionary

50
00:03:10,230 --> 00:03:13,560
and each of the temperatures have been converted to Fahrenheit.

51
00:03:13,890 --> 00:03:15,360
So this is super useful

52
00:03:15,360 --> 00:03:19,290
and you often find yourself needing to work with values in a dictionary.

