Manipulate Data with Dictionaries in Python

Will Button
InstructorWill Button
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 6 years ago

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.

egghead
egghead
~ 12 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today