By the way, there is actually one more way that we've actually seen in list how to look for an item
in a dictionary.
Remember how enlists 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 a 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 H.
Which is true.
But if I want to check a 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.
All right.
This is interesting.
I have dict items here and I grab looks like a list.
And here we have a bracket basket.
One, two, three.
Greet hello.
And age 20.
But there's some weird syntax here that we haven't seen before and this is actually a tuple.
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 H so I'm going to say H 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.
What just happened?
It randomly pops up something.
One of the keys and the values in this case, age 20 is removed.
So the last item on the dictionary gets removed by remember, a dictionary is unordered.
So 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.
An 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 255.
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 pages and I click run.
It will still update with a new key item.
So this is another really useful method.
All right.
That's it for dictionaries.
I'll see you in the next video.