Let's talk about set comprehension and dictionary comprehension.
Now this first one is actually quite easy, you see, because we can do the same thing that we've done
with lists.
We simply change these bracket notation to a set, which is a curly bracket like this, so that if I
do my list.
The first one, I get a set.
Same with the second one.
If I go like this.
My list to.
Just like that.
And I think you get the idea.
Let's do one more on the final one.
So my list for.
Again, you're able to do a set comprehension on sets as well.
A quick way for us to generate sets.
I remember sets only allow numbers that are not duplicate only unique items.
What about dictionary comprehension?
Well, let's do something like this.
Using the same formula we've used.
Let's create a my debt.
And in here, we're going to create a key and a value just like that.
And this key value.
Can be acted upon.
So in our case, let's say we want to multiply the value by two or to the power of two.
So this is what we want to create a key value pair and the value gets acted upon.
Now in here, I'm going to say another for loop for.
So let's say key value.
And then in.
Well.
And what?
What are we going to iterate over?
Well, let's think here.
In order for us to create a dictionary, we need both a key and a value.
Right.
So I need to pass it an iterable, but I need to pass it an iterable like a dictionary that has both
key and value.
So we can do something simple here.
We can say simple dictionary that will have.
A as one.
And be.
As to.
And now all we do is pass simple dictionaries in here.
As an iterable.
We grab the key and values by doing dot items, which we've seen before.
And let's make this a little bit bigger.
There you go.
Let's put it all the way.
Just so we're all clear.
It's all on one line.
And now let's just see what happens and we'll go through it step by step.
I'm going to copy this and paste it.
And if I click Run.
Look at that.
I have a new dictionary that has a equal to one and be equal to four.
Now, let's go through it one by one.
Just like we saw with list and set.
The first part is what we want to do with the data.
In our case, I wanted key and value, which I was going to extract here in our for loop.
I could have named this whatever I want though I could have done K and V and you might see this commonly
in python code just short term for key and value.
And then I wanted to do the power of two to the value.
So then this is what I wanted to do.
This is the action.
This is the four loop that we did.
So the for loop was, hey, extract the key and value using the dictionary dot items.
Remember I needed to have two values so I can fill in key and value.
And then the value you multiply by two or to the power of two.
So we get that.
If for example, I wanted to add an if statement and say, Hey, I only want you to add.
If the value.
Is even.
So let's say modulo two equals zero.
Only then I want you to add the dictionary if I click Run.
Only the even value for gets added to the dictionary.
Very, very cool.
Again, this makes your code a little bit less readable, so it's just a shorthand for you to use.
And maybe if you see it in code, you at least know what it is.
But you should be careful not to be overly clever and do the single line dictionary comprehension just
because you can.
Just to solidify our knowledge.
Let's finish up with another example.
What if instead I have this.
I want to do a four.
Number in a list.
One, two, three.
So we no longer have this dictionary, but we still need to include a key and a value pair.
Could I do it so that maybe the item is the key and the item multiplied by two is the value?
How would we do that?
Pause the video and give it a go if you want otherwise.
This is how we do it.
We simply say number is the key and the value will be num times two.
Just like that.
Nothing too difficult.
Right?
If I click run.
There we go.
We're able to use comprehension.
List comprehension set comprehension dictionary comprehension as a way to create lists with Python,
as a way to create these data structures really fast with Python.
Let's take a break and do an exercise in the next video.