Add value to Dictionary if Key already exists in Python

This tutorial will discuss how to add value to dictionary if key already exists in Python.

Suppose we have a dictionary, and it contains string names as keys and their mark as values. Like this,

student_data = {
    "Varun Sharma": 21,
    "Renuka Singh": 19,
    "Sachin Roy": 20
}

The type of value is integer. Now we want to add a new value into a dictionary if the given key already exists in the dictionary. Suppose the key “Sachin Roy” already had the value 20 linked with it in the Dictionary. Now we want to add an additional value 11 with that key. For that we have created a separate function,

def addValue(dictObj, key, value):
    # Check if the key exists in the dictionary
    if key in dictObj:
        # Check if the value is a list
        if isinstance(dictObj[key], list):
            # Append the value to the list
            dictObj[key].append(value)
        else:
            # Convert the value to a list and append the new value
            dictObj[key] = [dictObj[key], value]

In this function, we will pass the dictionary, the key and the additional value as arguments. First of all, it will check if the given key exists in the dictionary or not. If dictionary contains the key, then it will check what is the type of the associated value of that key. If the type of the existing value for the given key is a list type, then it will append the new value into it. Otherwise it will create a temporarily list with old value of given key, and the new value into it. Then it will assign this new list as a value of the given key in the dictionary.

Let’s use this function to add a value to an existing key in the dictionary,

# Define the key and value to add
key_to_add = "Sachin Roy"
value_to_add = 11

addValue(student_data, key_to_add, value_to_add)

Output:

{
    "Varun Sharma": 21,
    "Renuka Singh": 19,
    "Sachin Roy": [20, 11]
}

We can call the addValue() function and pass the dictionary as the first argument, second argument will be the existing key and the third argument will be in new value.

So it will add the new value into the existing key. Basically it will link the new value with the existing key, by converting the value associated with the key into a list.

Let’s see the complete example,

def addValue(dictObj, key, value):
    # Check if the key exists in the dictionary
    if key in dictObj:
        # Check if the value is a list
        if isinstance(dictObj[key], list):
            # Append the value to the list
            dictObj[key].append(value)
        else:
            # Convert the value to a list and append the new value
            dictObj[key] = [dictObj[key], value]

# Create a dictionary with initial values
student_data = {
    "Varun Sharma": 21,
    "Renuka Singh": 19,
    "Sachin Roy": 20
}

# Define the key and value to add
key_to_add = "Sachin Roy"
value_to_add = 11

addValue(student_data, key_to_add, value_to_add)

# Print the updated dictionary
print(student_data)


# Define the key and value to add
key_to_add = "Mansi Kumar"
value_to_add = 11

addValue(student_data, key_to_add, value_to_add)

# Print the updated dictionary
print(student_data)

Output

{'Varun Sharma': 21, 'Renuka Singh': 19, 'Sachin Roy': [20, 11]}
{'Varun Sharma': 21, 'Renuka Singh': 19, 'Sachin Roy': [20, 11]}

Summary

Today, we learned how to add value to dictionary if key already exists in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top