12.2. Working with Dictionaries

Dictionaries are mutable, so we can change the value assigned to a key, add new key/value pairs, or remove key/value pairs.

Since dictionaries are unordered, we have no options for sorting or rearranging the key/value pairs within the collections.

12.2.1. Change One Value

To update a single value in a dictionary, use the syntax:

dictionary_name[key] = new_value

dictionary_name[key] identifies the value in the collection that we want to change. The = operator assigns new_value to that key.

Example

1
2
3
4
5
6
7
8
9
phone_book = {
   'Mom' : '555-5555',
   'Work' : '555-5556',
   'Home' : '123-456-7890'
}
print(phone_book)

phone_book['Work'] = '314-555-5556'  # Link the key 'Work' to the value '314-555-5556'.
print(phone_book)

Console Output

{'Mom' : '555-5555', 'Work' : '555-5556', 'Home' : '123-456-7890'}
{'Mom' : '555-5555', 'Work' : '314-555-5556', 'Home' : '123-456-7890'}

Note

We cannot use this method to change the names of the keys.

12.2.2. Add a New Key/Value Pair

After defining a dictionary, we can add new new key/value pairs at any time by using bracket syntax:

dictionary_name['new_key'] = new_value

Example

1
2
3
4
5
6
7
8
9
phone_book = {
   'Mom' : '555-5555',
   'Work' : '555-5556',
   'Home' : '123-456-7890'
}
print(phone_book)

phone_book['BFF'] = '555-5557'
print(phone_book)

Console Output

{'Mom' : '555-5555', 'Work' : '555-5556', 'Home' : '123-456-7890'}
{'Mom' : '555-5555', 'Work' : '555-5556', 'Home' : '123-456-7890', 'BFF' : '555-5557'}

12.2.3. Remove a Key/Value Pair

To remove a key/value pair from a dictionary, use the del keyword. The general syntax is:

del dictionary_name[key]

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
phone_book = {
   'Mom' : '555-5555',
   'Work' : '555-5556',
   'Home' : '123-456-7890',
   'BFF' : '555-5557'
}
print(phone_book)

del phone_book['Home']
print(phone_book)

Console Output

{'Mom' : '555-5555', 'Work' : '555-5556', 'Home' : '123-456-7890', 'BFF' : '555-5557'}
{'Mom' : '555-5555', 'Work' : '555-5556', 'BFF' : '555-5557'}

Note

Once we define a key, it remains in the dictionary unless we use del to remove it.

For example, if we wanted to rename the key 'Mom' to 'Mother', we would have to delete the old key first, then add a new key/value pair.

 9
10
del phone_book['Mom']
phone_book['Mother'] = '555-5555'

12.2.4. Try It!

Example

Use a dictionary to keep track of favorite ice cream flavors!

In the editor below:

  1. Change the value for 'Mom' to something more exciting than 'vanilla'.
  2. Add your own name and favorite flavor to the dictionary.
  3. Use del to remove the 'Odd choice' key/value pair.
  4. Fix the misspelling in the 'teachher' key.

12.2.5. Check Your Understanding

Question

Given the following dictionary:

pet_population = {'cats' : 10, 'dogs' : 5, 'elephants' : 25}

What value does len(pet_population) return? (Hint: Try it in the code editor above).

  1. 3
  2. 6
  3. 40

Question

Using the same pet_population dictionary, what would the following statement do?

pet_population['birds'] = 5
  1. Throw an error message because pet_population does not contain a 'birds' key.
  2. Add the 'birds' : 5 key/value pair to the dictionary.
  3. Add five 'birds' keys to the dictionary.
  4. Replace the 'dogs' key with 'birds'.

Question

What is printed by the following statements?

1
2
3
4
5
pet_population = {'cats' : 10, 'dogs' : 5, 'elephants' : 25}

pet_population['mice'] = pet_population['cats'] + pet_population['dogs']

print(pet_population['mice'])
  1. 0
  2. 5
  3. 10
  4. 15

Question

Once we added mice to our pet collection, all the elephants ran away! Which of the following shows the correct syntax for setting the value of the 'elephants' key to zero?

  1. pet_population['elephants'] = 0
  2. pet_population['elephants'] = ''
  3. del pet_population['elephants']
  4. pet_population[2] = 0