Add Value to a key in a Python Dictionary

This tutorial will discuss how to add value to a key in a Python dictionary.

Suppose we have a dictionary which contains certain string as keys and integer and values. Like this,

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

Now we want to add a value to the existing key in the dictionary.

For example, key “Renuka Singh” has the value 19 linked with it. Now we want to add an integer value 5 into the value associated with the key “Renuka Singh”.

For this, we can use the get() method. First we will fetch the value associated with the key using the get() method and we will pass the default value as zero in get() method. It means if the key does not exist in the dictionary then it will return zero.

# Define the key and value to add
key_to_add = "Renuka Singh"
value_to_add = 5

# Add the value to the key in the dictionary
student_data[key_to_add] = student_data.get(key_to_add, 0) + value_to_add

Output:

{
    "Varun Sharma": 21,
    "Renuka Singh": 24,
    "Sachin Roy": 20
}

So in this case get() method will return the value associated with existing key. Then we will add integer value into it and assign it back to the reference of the value of the same key in dictionary, using the square brackets. It will update the value of the key by adding numeric value into it.

So, if we use the same logic and try to increment the value of a key that does not exist in the dictionary, then it that case it will add a new key value pair into the dictionary where the value to be added will be become the value field of that key value pair like this,

# Define the key and value to add
key_to_add = "Shamita Roy"
value_to_add = 5

# Add the value to the key in the dictionary
student_data[key_to_add] = student_data.get(key_to_add, 0) + value_to_add

# Print the updated dictionary
print(student_data)

Output:

{
    "Varun Sharma": 21,
    "Renuka Singh": 24,
    "Sachin Roy": 20,
    "Shamita Roy": 5
}

Let’s see the complete example,

# 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 = "Renuka Singh"
value_to_add = 5

# Add the value to the key in the dictionary
student_data[key_to_add] = student_data.get(key_to_add, 0) + value_to_add

# Print the updated dictionary
print(student_data)


# Define the key and value to add
key_to_add = "Shamita Roy"
value_to_add = 5

# Add the value to the key in the dictionary
student_data[key_to_add] = student_data.get(key_to_add, 0) + value_to_add

# Print the updated dictionary
print(student_data)

Output

{'Varun Sharma': 21, 'Renuka Singh': 24, 'Sachin Roy': 20}
{'Varun Sharma': 21, 'Renuka Singh': 24, 'Sachin Roy': 20, 'Shamita Roy': 5}

Summary

Today, we learned how to add value to a key in a Python dictionary.

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