Python Dictionary Update

In this Python tutorial, you will learn How to update a dictionary in Python in different ways with practical examples.

While working on a Python project, the employee data was stored in JSON format, which can be converted into a dictionary in Python. So, we needed to add employee email to the dictionary in Python.

In this type of situation, we can update the dictionary in Python in multiple different ways.

How to Update Dictionary in Python

Let’s understand the scenario more clearly so you can use both approaches correctly in Python.

emp_1 = {"name": "John",
            "age": 24,
            "salary": 18000}

Suppose I have this emp data and need to update the salary from 18000$ to 20000$.

Let’s see how we can update the dictionary in Python in different ways.

Python Dictionary Update by Adding New Key and Value using update() method

First, we will use the update() method in Python, which is the direct way to update the dictionary in Python. The update () method is used to add a new key and value, or if the key already exists, then it can make changes in the value of that key.

Syntax

dict.update({key:value})
  • dict.update({key: value}): If the key doesn’t exist in the dict, it will create a new one.

Let’s understand How to update() method works with the dictionary in Python

emp_1 = {"name": "John",
        "age": 24,
        "salary": 18000}

emp_1.update({"email":"john@example.com"})

print(emp_1)
Update Dict using the setdefault() Method in Python

In the above code, we have a dictionary with name, age, and salary. And we needed to add email to the dictionary.

READ:  How to Remove Special Characters Except for Space from a String in Python [9 Ways]

So, we used the update() method to make changes to the dictionary like this:
emp_1.update({“email”:”john@example.com”})
. It will add email as a key and john@example.com as a value in Python.

Update Dict using the setdefault() Method in Python

We can also use the setdefault() method in Python, which is a built-in method to add new keys to the dictionary in Python. It will work only when the key does not exist; if it exists, it will not make any changes to the dictionary.

Syntax

dict.setdefault("key", "value")

Let’s execute a practical example to update dict in Python using the setdefault() method in Python.

emp_1 = {"name": "John",
        "age": 24,
        "salary": 18000}

emp_1.setdefault("age", 30)

emp_1.setdefault("is_active", True)

print(emp_1)
Update Dict using the setdefault() Method in Python

In the above code, we used the setdefault() method, like this emp_1.setdefault(“is_active”, True) will add the new key as is_active and the default value as True. We also gave an example for age, which exists in the dict, so it will not change values.

Dict Update Python by Assigning a Value to the Key Directly

We can directly update the dictionary by assigning the key and value in Python. First, we will give the dictionary name and then access the value by giving the key name in the bracket.

If the key already exists, it will update the value, and if it does not, it will create a new key in the dictionary.

Syntax

dict[key] = value

Let’s understand the practical example of updating a dictionary in Python

emp_1 = {"name": "John",
        "age": 24,
        "salary": 18000}

emp_1["is_active"] = True

print(emp_1)
Dict Update Python by Assigning a Value to the Key Directly

In the above code, we assigned a new key to the dictionary and gave a value like this emp_1[“is_active”] = True, so it will update the dictionary by creating a new key into the dictionary in Python.

READ:  Python Find Last Number in String

Conclusion

In this Python article, you learn how to update the Python dictionary in multiple ways, such as using the update() method, then the setdefault() method, and assigning new key values directly in the dictionary.