Dictionaries

Modifying Python Dictionaries

In this lesson, you will learn how to modify existing dictionaries by updating, adding, and deleting key-value pairs.


Let's create a new dictionary for a guy named Bob.

person = { 'name': 'Bob', 'age': 30 }
Python

Now assume we learned that Bob is a data scientist and want to add this information to our dictionary.

New key-value pairs can be added to a dictionary through direct assignment, like so:

dict[key] = value
Python

Let's update our dictionary with the new information about Bob:

person = { 'name': 'Bob', 'age': 30 } person['job'] = 'data scientist' print(person.get('job'))
Python
Output

Another way to add new information to an existing dictionary is by using the dict.update() method:

person = { 'name': 'Bob', 'age': 30 } person.update({'job': 'data scientist'}) print(person.get('job'))
Python
Output

Keep in mind that you should pass a separate dictionary as an argument to the dict.update() method.

With dict.update(), you can also add multiple key-value pairs at once:

person = { 'name': 'Bob', 'age': 30 } person.update({'job': 'data scientist', 'gender': 'male'}) print(person.get('job')) print(person.get('gender'))
Python
Output

We have added Bob's gender and his job at the same time.


What will be the output?

my_dict = { 'a': 1, 'b': 2 } my_dict['c'] = 3 print(my_dict['c'])
Python

What will be the output?

my_dict = { 'a': 1, 'b': 2 } my_dict[3] = 'c' print(my_dict[3])
Python

What will be the output?

my_dict = { 'a': 1, 'b': 2 } my_dict.update({'c': 2}) print(my_dict['c'])
Python

Back to Bob.

person = { 'name': 'Bob', 'age': 30 }
Python

Let's assume that Bob just turned 31, and we need to update his age in our dictionary.

We can update a key-value pair in the same way as we add a new one, through direct assignment:

person = { 'name': 'Bob', 'age': 30 } person['age'] = 31 print(person.get('age'))
Python
Output

dict.update() can also update a key-value pair:

person = { 'name': 'Bob', 'age': 30 } person.update({'age': 31}) print(person.get('age'))
Python
Output

That's because the dictionary that you pass as argument to dict.update() takes precedence over the existing one.


What will be the output?

my_dict = { 'a': 1, 'b': 2 } my_dict.update({'b': 3, 'c': 2}) print(my_dict)
Python

What will be the output?

my_dict = { 'a': 1, 'b': 2 } my_dict['b'] = 3 print(my_dict['b'])
Python

Awesome. Now we just need to learn how to delete existing key-value pairs.

Key-value pairs can be deleted using the del keyword:

person = { 'name': 'Bob', 'age': 31 } # Remove the key-value pair with key 'age' del person['age'] print(person)
Python
Output

You can also use the pop() method, which we already know from its usage with strings and lists.

dict.pop(key) removes the key-value pair associated with the specified key and returns the value:

person = { 'name': 'Bob', 'age': 31 } # Remove and return the value of 'age' age = person.pop('age') print(person) print(age)
Python
Output

What will be the output?

my_dict = { 'a': 1, 'b': 2 } del my_dict['b'] print(my_dict)
Python

What will be the output?

my_dict = { 'a': 1, 'b': 2 } del my_dict['b'] print(my_dict['b'])
Python