12.3. Dictionary Methods

As with strings and lists, Python provides us with some useful methods for dictionaries. These methods will either change an existing dictionary, return information about the dictionary, or create and return a new dictionary.

12.3.1. Common Dictionary Methods

Here is a sample of the most frequently used dictionary methods. A more complete list can be found at W3Schools.

To see detailed examples for a particular method, click on its name.

Common Dictionary Methods
Method Syntax Description
clear dictionary_name.clear() Removes all key/value pairs from a dictionary.
copy dictionary_name.copy() Returns an independent copy of a dictionary. This is also called cloning.
pop dictionary_name.pop(key) Removes the selected key/value pair from the dictionary and returns the value.
keys dictionary_name.keys() Returns all of the key names in the dictionary, which can then be moved into a list.
values dictionary_name.values() Returns all of the values in the dictionary, which can then be moved into a list.
items dictionary_name.items() Returns all of the key/value pairs in the dictionary, which can then be moved into a list.

Note

Since dictionaries are unordered, we have no need for methods that sort the key/value pairs.

12.3.2. max, min, and len

Just like with strings and lists, the len() function returns the number of items in a dictionary. Note that each key/value pair gets counted as a single item.

Try It!

  1. Run the code as-is first to see the results.
  2. Add characters to the text string, elements to the words list, and new key/value pairs to state_capitals.
  3. Run the code again to see how len() counts the entries for each collection.

max() and min() provide similar results for dictionaries as they do for strings and lists. By default, the functions return the largest or smallest KEY in the dictionary, not value. As a best practice, we should specify if we are looking for a largest/smallest key or value.

1
2
3
4
max(dictionary_name.keys())    # Returns the largest key from the dictionary.
min(dictionary_name.values())  # Returns the smallest value in the dictionary.

min(dictionary_name)           # Returns the smallest key from the dictionary.

Note

Remember that for strings, max() and min() return values based on their position in the alphabet, with capital letters coming before lowercase.

Using this model, Python considers "Zebra" larger than "Hippo" but smaller than "apple".

Try It!

  1. Print the maximum and minimum keys from the num_animals dictionary.
  2. Print the maximum and minimum values from the dictionary.
  3. What happens if you use the .items() method instead of .keys() or .values()?

12.3.3. Check Your Understanding

As you answer these questions, follow the links given in the method table as needed.

Question

What is printed by the following statements?

1
2
3
4
num_animals = {"cats":12, "dogs":6, "elephants":23, "bears":20}

removed = num_animals.pop('elephants')
print(removed)
  1. elephants
  2. 23
  3. {"cats":12, "dogs":6, "bears":20}

Question

What is printed by the following statements?

1
2
3
num_animals = {"cats":12, "dogs":6, "elephants":23, "bears":20}

print(min(num_animals))
  1. bears
  2. dogs
  3. 6
  4. 20

Question

What is printed by the following statements?

1
2
3
4
5
6
num_animals = {"cats":12, "dogs":6, "elephants":23, "bears":20}

key_list = list(num_animals.keys())
key_list.sort()

print(key_list[3])
  1. cats
  2. dogs
  3. elephants
  4. bears