Manipulate Data with Dictionaries in Python

Will Button
InstructorWill Button
Share this video with your friends

Social Share Links

Send Tweet

Dictionaries may be familiar to you as hash maps. In this lesson, you will learn how to create them, get the values, and delete elements from the dictionary.

Instructor: I can create a dictionary by specifying the variable name, and then a set of empty braces. You may know dictionary as a hashmap, as well.

If I want to add an item to it, I'll use a square bracket and the key, because these are key-value pairs, and then the value I want to assign to that key. I can do this for a couple more examples, and then if I print out the dictionary, it shows the three key-value pairs that I added to it.

If I want to just retrieve an individual value from the list, I specify the name of the dictionary, the value or the key that I want to retrieve from it, and then it returns the value.

I can do the same thing if I want to retrieve the age for the key Bob. I'll specify age, the key name that I'm looking for, and the value gets returned.

I can use the in operator to see if a key exists. If I want to see if the key will exist in the dictionary age, it returns true, but if I specify the name of a key that doesn't exist, it returns false.

Dictionaries also have a get method. I can do age.get, and then request the key name that I want, and then there's an optional string that we can append on there of what the get method will return if that key is not found.

In this case, Will was found, so it returned the value. If I use the delete method to delete the key named Will and then run that same command, it prints out the not found string that we applied.

If I want to iterate over all of the key value pairs in the dictionary, I can specify the variable names for my key and my value, and then specify in the dictionary, and then call the items operator to iterate over it.

Then we'll use our print command. Just print the two variables key and value, and it prints them out.