-: By the way
there is actually one more way that we've actually seen
in lists how to look for an item in a dictionary.
Remember how in lists we use the keyword, "in."
We said something along the lines of,
hey, is i in a list
that contains i?
Or we even use a string like, hi.
Well, you can do the same with dictionaries.
I can say, does basket exist in user?
And let's print this out,
and run.
And it's going to say, yep, basket does exist in user.
What about,
what about size?
Does that exist?
Nope, that's false.
That does not exist in user.
But here is where it gets really interesting.
And this, again,
is another dictionary method that we can use.
And by the way, you can see that there's not
that many dictionary methods that, well, dictionaries have.
Luckily we're going to go over the main ones here.
The one is called "keys",
and keys simply checks the keys.
What are the keys of user?
So if I go, hello here, and I click run,
well, no, keys are sorry on this side, not this.
So this is a value.
So if I want to check keys, I'd say age.
Which is true.
But if I want to check values, well, in that case
if I do, hello, I get true.
It's going to go over
or what we call iterate over the values.
Another one that's really useful is the items.
And items is special because we've seen keys and values
but items actually grabs the entire item.
So the way we can actually see this is
let's just remove
and click run.
Alright, this is interesting.
I have dict items here and I grab, looks like a list.
And in here we have a bracket basket
1, 2, 3, greet, hello, and age 20.
But there's some weird syntax here
that we haven't seen before, and this is actually a couple.
So we're going to hold off on this and talk
about this a little bit more when we talk
about our next data structure.
But keys, values, and items, is an easy way
for us to grab these things for us from our dictionary.
Let's continue on
with other dictionary methods that we might use.
One is a very nice one, it's called, "clear."
And if I run this,
you see that I get an empty dictionary.
What if I just run users dot clear here and run user?
Well, it's still an empty object
or an empty dictionary.
So you can see here that clear doesn't actually
return anything.
It doesn't, well, it's just in place,
removes whatever the dictionary has.
So it just creates an empty dictionary.
What about copy?
Well, a copy allows us to copy a user
so that if I print user
and user two
and I click run.
I get two users each keys and values copied.
But if I now,
let's say clear the first user
and I click run,
the first user is empty
but because I've copied the second user,
the second user has the old information.
And this is a concept that we've talked about before.
Another useful command
and let's just go back to having user,
is user dot pop
which removes a key and a value from the dictionary.
So in our case,
let's just say we want to remove the key that is age.
So I'm going to say age, and if I run this,
I get 20
because pop returns the value of whatever got removed.
But if I do print user now, you'll see that age does not
exist anymore because we've popped it off.
But pop, as you can see, removes the actual value,
or returns the actual value, 20.
Here's another really fun one, pop item.
And honestly, this I haven't used much,
but I just think it's a fun one.
Pop item, if you guessed, well, you're not going to guess.
Oh, and that's because
I have to make sure I write it
properly with lowercase.
If I run this,
which just happened.
It randomly pops off something.
One of the keys and the values.
In this case, age 20 is removed.
So the last item on the dictionary gets removed.
But remember, a dictionary is an order.
So if this was a massive, massive dictionary
that, well, doesn't have any order to it,
sometimes it removes some pair.
As you can see, not the last one,
but some pair of key value.
So you have to be careful with this one.
It doesn't necessarily just remove the last thing
that you entered, but it might be useful on some occasions.
Finally, we have something called, "update."
And update simply updates,
as the name suggests, a key value.
So let's have a look here.
All we have to do is give it a new dictionary.
So curly brackets, and just simply say,
hey I want to update age to 55.
If I click run,
oh, and I forgot a bracket here.
Let's try that again.
If I click run,
there you go.
Age got updated.
But if this was a key that doesn't exist like ages
and I click run, it will still update with a new key item.
So this is another really useful method.
Alright, that's it for dictionaries.
I'll see you in the next video.