How to Update Values in a Python Dictionary?

In this tutorial, I will explain how to update values in a Python dictionary. As a developer, I encounter situations where I need to modify existing values in a dictionary, then I explored various methods to update dictionary values effectively. I will share my findings along with suitable examples and screenshots.

Update Values in a Python Dictionary

Let’s consider a real-world scenario. Suppose you’re working on a project that involves managing customer information for a company based in the United States. You have a Python dictionary called customer_info that stores customer details like name, age, and city. Here’s an example:

customer_info = {
    "John Doe": {"age": 30, "city": "New York"},
    "Jane Smith": {"age": 25, "city": "Los Angeles"},
    "Michael Johnson": {"age": 35, "city": "Chicago"}
}

Now, let’s say you need to update the age of “John Doe” and the city of “Jane Smith”. How can you achieve this?

Read How to Change a Key in a Dictionary in Python?

Method 1: Use Square Brackets Notation

One way to update a dictionary value is by using square brackets notation. You can access the specific key-value pair you want to modify and assign a new value to it. Here’s an example:

customer_info["John Doe"]["age"] = 31
customer_info["Jane Smith"]["city"] = "San Francisco"

Output:

{'John Doe': {'age': 31, 'city': 'New York'}, 'Jane Smith': {'age': 25, 'city': 'San Francisco'}, 'Michael Johnson': {'age': 35, 'city': 'Chicago'}}

I executed the above example code and added the screenshot below.

Update Values in a Python Dictionary

In the above code, we directly accessed the “age” key of “John Doe” and the “city” key of “Jane Smith” using square brackets and assigned new values to them.

Check out Python Get First Key in Dictionary

Method 2: Use the update() Method

Another approach to updating dictionary values is by using the update() method. This method allows you to update multiple key-value pairs at once by passing a dictionary or an iterable object with key-value pairs.

customer_info["John Doe"].update({"age": 31})
customer_info["Jane Smith"].update({"city": "San Francisco"})

Output:

{'John Doe': {'age': 31, 'city': 'New York'}, 'Jane Smith': {'age': 25, 'city': 'San Francisco'}, 'Michael Johnson': {'age': 35, 'city': 'Chicago'}}

I executed the above example code and added the screenshot below.

How to Update Values in a Python Dictionary

In this example, we used the update() method to modify the “age” of “John Doe” and the “city” of “Jane Smith”. The update() method takes a dictionary as an argument, where the keys represent the keys to be updated, and the corresponding values represent the new values.

Read How to Get All Values From Dictionary Python

Handle KeyError Exception

When updating dictionary values, it’s essential to handle the KeyError exception that may occur if the key you’re trying to update doesn’t exist in the dictionary. You can use the get() method or the in operator to check if a key exists before updating its value.

if "Robert Wilson" in customer_info:
    customer_info["Robert Wilson"]["age"] = 40
else:
    print("Customer not found.")

Output:

Customer not found.

I executed the above example code and added the screenshot below.

Update Values in a Python Dictionary handle KeyError Exception

In this case, we first check if the key “Robert Wilson” exists in the customer_info dictionary using the in operator. If it does, we proceed with updating the “age” value. Otherwise, we print a message indicating that the customer was not found.

Check out How to Get the Length of a Dictionary in Python?

Conclusion

In this article, I explained how to update values in a Python dictionary. I discussed mainly two methods to accomplish this task such as using square brackets notation , and using the update() method. I also covered how to handle KeyError exceptions.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.