Well let's talk about set comprehension and dictionary comprehension.
Now this first one is actually quite easy to see because we can do the same thing that we've done with
lists.
We simply change these bracketed 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 set comprehension on sets as well.
A quick way for us to generate sets and 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 dict and then
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 four loop four.
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 and it's horrible but I need to pass it an extra bowl 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 B as two and now all we do is pass a simple dictionary in here as an admirable.
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
the 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 in if I click Run.
Look at that.
I have a new dictionary that has a equal to 1 and b equal to 4.
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 envy 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 for loop that we did.
So the for loop was hey extract the key in 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 multiplied
by two or two 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 that 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 these single line dictionary comprehension
just because you can't just 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 no.
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 as list comprehension is set comprehension.
Dictionary comprehension is 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.