Instructor: All right, in this exercise,
we're going to be doing some dictionary comprehension.
What we have is in the input a sentence
and when that sentence is passed in into your main.py,
you're going to convert it into a list of words.
And the list should contain each word in the sentence.
And you're gonna use that list to then create a dictionary
where you count the number of letters in each word.
If you take a look in the description pane,
you'll see what the example output should look like
for the input sentence
what is the airspeed velocity of an unladen swallow?
And you'll notice that for simplicity's sake,
we are counting all punctuation as a part of the word.
So the swallow and question mark at the end
of that example output dictionary is counted
as eight and not seven.
This is the goal
and you're gonna need to use what you've learned
about list and dictionary comprehension.
You might have to google around to figure out
how to turn a sentence into a list of words
and you might need to check some
of the hints to help you along the way.
Have a go at this challenge.
Similar to list comprehension, we start with the for loop.
So notice that we are creating a dictionary comprehension
and the only difference between dictionary
and list comprehension is the symbol
that goes around our code.
For the list, it was the square bracket,
for dictionary, it's the curly braces.
The first thing we have is sentence.split
and this is a Python method that can take a string
and split it.
Inside the split method, we can add an optional separator.
So what do you wanna split the sentence by?
And if you leave that empty, then it's going to split it
by white space, which is what we have here.
The space between each word is going to be used
as the marker that is going to define each item in the list.
Sentence.split basically takes our input sentence
and then creates a list adding each item separated
by white space as an individual new element in the list.
And you can run the code at this point to see
what that looks like if you haven't managed
to get this to work.
Now, the next step in our dictionary comprehension
is our for loop.
So we're saying for word in sentence.split,
which means let's loop through each of the items
in that list we've just created, and we're going
to do something to each of the items in the list,
which we can now access through this variable name word.
Finally, at the beginning of our dictionary comprehension,
we define what is going to go
into our dictionary, the structure of it, if you will.
It's going to have a key and value pair.
The key is going to be the actual word.
And notice this comes from our for loop.
So this is equivalent to each item in our list.
Every single word basically
in the sentence is going to be a key
and then the value comes after the colon symbol.
And this is using the len method to calculate the length
of that word.
So how many characters are in that word
and this entire line of code achieves that objective
that we had in the beginning of the exercise,
which is to create a dictionary by taking an input sentence
and show us how many characters are in each word.
And each word is separated
as a separate key-value pair in our dictionary.
Hopefully you managed to get this to work.
If not, go back now and fix your code.
Don't worry if you didn't manage to get it
or you see the solution and it looks different.
If your code managed to pass the test,
then it has achieved the objective
even if it looks different.
But if your code didn't work,
then this is the learning point.
This is the time to fix it
and get this skill into your tool belt.
So don't worry about it.