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.
Method |
Syntax |
Description |
---|---|---|
|
Removes all key/value pairs from a dictionary. |
|
|
Returns an independent copy of a dictionary. This is also called cloning. |
|
|
Removes the selected key/value pair from the dictionary and returns the value. |
|
|
Returns all of the key names in the dictionary, which can then be moved into a list. |
|
|
Returns all of the values in the dictionary, which can then be moved into a list. |
|
|
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!
Run the code as-is first to see the results.
Add characters to the
text
string, elements to thewords
list, and new key/value pairs tostate_capitals
.Run the code again to see how
len()
counts the entries for each collection.
1 2 3 4 5 6 7 8 9 10 11 12 | text = "What does the fox say?"
words = ['What', 'does', 'the', 'fox', 'say?']
state_capitals = {
'MO' : 'Jefferson City',
'CA' : 'Sacramento',
'TX' : 'Austin',
'HI' : 'Honolulu'
}
print(len(text))
print(len(words))
print(len(state_capitals))
|
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!
Print the maximum and minimum keys from the
num_animals
dictionary.Print the maximum and minimum values from the dictionary.
What happens if you use the
.items()
method instead of.keys()
or.values()
?
1 2 3 4 5 6 7 8 9 10 | num_animals = {
'lions' : 3,
'tigers' : 2,
'bears' : 8,
'pigeons' : 3000,
'snakes' : 37,
'Koalas' : 3
}
# Code your four print statements here:
|
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)
|
elephants
23
{“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))
|
bears
dogs
6
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])
|
cats
dogs
elephants
bears