1
00:00:00,630 --> 00:00:02,250
Instructor: All right, in this exercise,

2
00:00:02,250 --> 00:00:05,550
we're going to be doing some dictionary comprehension.

3
00:00:05,550 --> 00:00:09,030
What we have is in the input a sentence

4
00:00:09,030 --> 00:00:13,680
and when that sentence is passed in into your main.py,

5
00:00:13,680 --> 00:00:18,533
you're going to convert it into a list of words.

6
00:00:19,470 --> 00:00:24,470
And the list should contain each word in the sentence.

7
00:00:24,870 --> 00:00:28,590
And you're gonna use that list to then create a dictionary

8
00:00:28,590 --> 00:00:32,820
where you count the number of letters in each word.

9
00:00:32,820 --> 00:00:35,370
If you take a look in the description pane,

10
00:00:35,370 --> 00:00:38,400
you'll see what the example output should look like

11
00:00:38,400 --> 00:00:40,620
for the input sentence

12
00:00:40,620 --> 00:00:44,100
what is the airspeed velocity of an unladen swallow?

13
00:00:44,100 --> 00:00:46,650
And you'll notice that for simplicity's sake,

14
00:00:46,650 --> 00:00:50,880
we are counting all punctuation as a part of the word.

15
00:00:50,880 --> 00:00:54,480
So the swallow and question mark at the end

16
00:00:54,480 --> 00:00:57,240
of that example output dictionary is counted

17
00:00:57,240 --> 00:01:00,030
as eight and not seven.

18
00:01:00,030 --> 00:01:01,530
This is the goal

19
00:01:01,530 --> 00:01:03,270
and you're gonna need to use what you've learned

20
00:01:03,270 --> 00:01:05,700
about list and dictionary comprehension.

21
00:01:05,700 --> 00:01:07,080
You might have to google around to figure out

22
00:01:07,080 --> 00:01:11,010
how to turn a sentence into a list of words

23
00:01:11,010 --> 00:01:12,090
and you might need to check some

24
00:01:12,090 --> 00:01:14,790
of the hints to help you along the way.

25
00:01:14,790 --> 00:01:16,983
Have a go at this challenge.

26
00:01:20,640 --> 00:01:24,330
Similar to list comprehension, we start with the for loop.

27
00:01:24,330 --> 00:01:27,990
So notice that we are creating a dictionary comprehension

28
00:01:27,990 --> 00:01:29,640
and the only difference between dictionary

29
00:01:29,640 --> 00:01:32,130
and list comprehension is the symbol

30
00:01:32,130 --> 00:01:34,470
that goes around our code.

31
00:01:34,470 --> 00:01:36,210
For the list, it was the square bracket,

32
00:01:36,210 --> 00:01:38,403
for dictionary, it's the curly braces.

33
00:01:39,660 --> 00:01:42,810
The first thing we have is sentence.split

34
00:01:42,810 --> 00:01:46,350
and this is a Python method that can take a string

35
00:01:46,350 --> 00:01:47,910
and split it.

36
00:01:47,910 --> 00:01:52,230
Inside the split method, we can add an optional separator.

37
00:01:52,230 --> 00:01:56,370
So what do you wanna split the sentence by?

38
00:01:56,370 --> 00:01:59,040
And if you leave that empty, then it's going to split it

39
00:01:59,040 --> 00:02:01,590
by white space, which is what we have here.

40
00:02:01,590 --> 00:02:04,650
The space between each word is going to be used

41
00:02:04,650 --> 00:02:09,650
as the marker that is going to define each item in the list.

42
00:02:10,080 --> 00:02:13,350
Sentence.split basically takes our input sentence

43
00:02:13,350 --> 00:02:17,460
and then creates a list adding each item separated

44
00:02:17,460 --> 00:02:22,200
by white space as an individual new element in the list.

45
00:02:22,200 --> 00:02:24,750
And you can run the code at this point to see

46
00:02:24,750 --> 00:02:27,090
what that looks like if you haven't managed

47
00:02:27,090 --> 00:02:28,233
to get this to work.

48
00:02:29,070 --> 00:02:31,950
Now, the next step in our dictionary comprehension

49
00:02:31,950 --> 00:02:33,480
is our for loop.

50
00:02:33,480 --> 00:02:36,930
So we're saying for word in sentence.split,

51
00:02:36,930 --> 00:02:39,930
which means let's loop through each of the items

52
00:02:39,930 --> 00:02:42,840
in that list we've just created, and we're going

53
00:02:42,840 --> 00:02:45,720
to do something to each of the items in the list,

54
00:02:45,720 --> 00:02:49,650
which we can now access through this variable name word.

55
00:02:49,650 --> 00:02:52,770
Finally, at the beginning of our dictionary comprehension,

56
00:02:52,770 --> 00:02:55,200
we define what is going to go

57
00:02:55,200 --> 00:02:58,740
into our dictionary, the structure of it, if you will.

58
00:02:58,740 --> 00:03:01,050
It's going to have a key and value pair.

59
00:03:01,050 --> 00:03:04,110
The key is going to be the actual word.

60
00:03:04,110 --> 00:03:06,300
And notice this comes from our for loop.

61
00:03:06,300 --> 00:03:11,130
So this is equivalent to each item in our list.

62
00:03:11,130 --> 00:03:12,960
Every single word basically

63
00:03:12,960 --> 00:03:16,230
in the sentence is going to be a key

64
00:03:16,230 --> 00:03:19,140
and then the value comes after the colon symbol.

65
00:03:19,140 --> 00:03:24,090
And this is using the len method to calculate the length

66
00:03:24,090 --> 00:03:24,923
of that word.

67
00:03:24,923 --> 00:03:27,870
So how many characters are in that word

68
00:03:27,870 --> 00:03:32,520
and this entire line of code achieves that objective

69
00:03:32,520 --> 00:03:35,040
that we had in the beginning of the exercise,

70
00:03:35,040 --> 00:03:39,300
which is to create a dictionary by taking an input sentence

71
00:03:39,300 --> 00:03:43,860
and show us how many characters are in each word.

72
00:03:43,860 --> 00:03:45,870
And each word is separated

73
00:03:45,870 --> 00:03:50,130
as a separate key-value pair in our dictionary.

74
00:03:50,130 --> 00:03:52,500
Hopefully you managed to get this to work.

75
00:03:52,500 --> 00:03:55,530
If not, go back now and fix your code.

76
00:03:55,530 --> 00:03:58,710
Don't worry if you didn't manage to get it

77
00:03:58,710 --> 00:04:02,250
or you see the solution and it looks different.

78
00:04:02,250 --> 00:04:04,410
If your code managed to pass the test,

79
00:04:04,410 --> 00:04:06,240
then it has achieved the objective

80
00:04:06,240 --> 00:04:08,040
even if it looks different.

81
00:04:08,040 --> 00:04:09,600
But if your code didn't work,

82
00:04:09,600 --> 00:04:11,070
then this is the learning point.

83
00:04:11,070 --> 00:04:13,320
This is the time to fix it

84
00:04:13,320 --> 00:04:16,470
and get this skill into your tool belt.

85
00:04:16,470 --> 00:04:17,822
So don't worry about it.

