1
00:00:00,300 --> 00:00:00,630
All right.

2
00:00:00,630 --> 00:00:04,620
It's time to put what you've learned to practice and see if you can complete

3
00:00:04,620 --> 00:00:07,110
these challenges on dictionary comprehension.

4
00:00:07,710 --> 00:00:12,690
So I want you to find the exercise that is day 26.4.

5
00:00:13,350 --> 00:00:17,190
Now, once you've found day 26.4 dictionary comprehension one,

6
00:00:17,460 --> 00:00:21,030
you'll see that this is the starting code I have for you.

7
00:00:21,030 --> 00:00:22,530
I've got a sentence here

8
00:00:22,710 --> 00:00:26,640
which is a string and your job is to count

9
00:00:26,730 --> 00:00:31,350
how many letters there are in each word and create a dictionary that looks like

10
00:00:31,350 --> 00:00:32,183
this.

11
00:00:32,220 --> 00:00:36,060
So your final dictionary is going to be saved as this variable called

12
00:00:36,060 --> 00:00:38,700
result and it's going to be printed to the console.

13
00:00:39,420 --> 00:00:41,130
Have a read of the instructions,

14
00:00:41,490 --> 00:00:45,270
especially be aware that you might need to do some Googling to figure out how to

15
00:00:45,270 --> 00:00:49,620
convert a sentence into a list of words and use the hints if you need them.

16
00:00:50,070 --> 00:00:52,230
So pause the video and give that a go now.

17
00:00:53,300 --> 00:00:54,133
Okay.

18
00:00:57,170 --> 00:01:01,010
The biggest hurdle of this challenge is not actually the dictionary 

19
00:01:01,010 --> 00:01:01,843
comprehension.

20
00:01:02,210 --> 00:01:07,210
It's the figuring of out how to convert a sentence, a string, into a list of words.

21
00:01:08,360 --> 00:01:13,190
So I've basically just typed out the entire query with Python at the end into

22
00:01:13,190 --> 00:01:17,060
Google. And if I click on the first Stack Overflow answer,

23
00:01:17,330 --> 00:01:22,250
you can see that they're telling you that you should probably investigate the split

24
00:01:22,310 --> 00:01:23,143
method.

25
00:01:23,450 --> 00:01:28,450
The split method basically splits a string by space and converts it into a list.

26
00:01:28,910 --> 00:01:32,450
That's exactly what we need in order to solve this challenge.

27
00:01:32,930 --> 00:01:37,930
And also in the hints, you can see that I've linked it to a page in the W3

28
00:01:38,240 --> 00:01:40,400
Schools documentation for Python.

29
00:01:40,850 --> 00:01:43,970
And I like looking to the W3Schools just because you have the

30
00:01:43,970 --> 00:01:45,080
try-it-yourself section

31
00:01:45,080 --> 00:01:50,080
where you can actually run the code and see what is the result of running this

32
00:01:50,570 --> 00:01:53,600
method. So if you have a sentence, Welcome to the jungle,

33
00:01:53,630 --> 00:01:55,250
once you call split on it,

34
00:01:55,490 --> 00:01:59,780
it creates a list with each word as a separate item in the list.

35
00:02:00,530 --> 00:02:03,680
Now we're basically ready to tackle our challenge.

36
00:02:04,520 --> 00:02:09,140
We're going to create our result which is going to be a dictionary.

37
00:02:09,620 --> 00:02:14,420
So let's start out with the keyword methods. So we'll say new_key:

38
00:02:14,450 --> 00:02:19,450
new_value for item in list.

39
00:02:20,570 --> 00:02:21,470
Now, in this case,

40
00:02:21,500 --> 00:02:26,500
we're not actually iterating through a dictionary and we're only iterating

41
00:02:27,140 --> 00:02:29,780
through a string, which is just a sequence.

42
00:02:30,200 --> 00:02:35,150
So we can replace this list with our sentence, but we can't leave it at

43
00:02:35,180 --> 00:02:40,010
just the sentence, because then it's going to loop through each of the letters.

44
00:02:40,520 --> 00:02:44,120
So instead of having it as just the sentence string,

45
00:02:44,210 --> 00:02:49,210
we're going to use the split method to convert it into a list of words.

46
00:02:50,390 --> 00:02:53,540
And once we've done that, then every time we loop through it,

47
00:02:53,600 --> 00:02:58,040
we're actually looping through each word in this list.

48
00:02:58,930 --> 00:03:01,840
And it's usually a good idea to test out the code.

49
00:03:01,870 --> 00:03:05,020
So let's comment out these last two lines of code,

50
00:03:05,380 --> 00:03:10,380
and then let's take our sentence and let's say for word in sentence.split.

51
00:03:13,060 --> 00:03:17,560
And now let's print out each of the words. And when I run this,

52
00:03:17,590 --> 00:03:22,590
you can see it has split our entire sentence by the spaces to create a list of

53
00:03:23,590 --> 00:03:27,760
words. And each time it iterates through it using for word in sentence.split

54
00:03:27,790 --> 00:03:28,960
just like we have here,

55
00:03:29,260 --> 00:03:34,000
it's going to go through each word in turn and then check each of them.

56
00:03:34,720 --> 00:03:39,720
So now let's reactivate our code. And we wanna fix the very last part.

57
00:03:40,960 --> 00:03:44,590
So the new key is going to be the actual word

58
00:03:44,650 --> 00:03:49,650
because we want each of the words in our dictionary as the key, and the new value

59
00:03:49,690 --> 00:03:54,040
is going to be the length of the word. So now when I hit run,

60
00:03:54,100 --> 00:03:59,100
you can see we've got a dictionary where each word is the key and the number of

61
00:03:59,320 --> 00:04:02,200
characters in each word is the value.

62
00:04:02,920 --> 00:04:04,810
And now I've completed the challenge.

