Increment value in dictionary - Python
Last Updated :
27 Jun, 2025
In Python, dictionaries store data as key–value pairs. If a key already exists, its value can be updated or incremented. This is commonly used for counting occurrences, like word frequency or item counts.
Let's discuss certain ways in which this task can be performed.
Using defaultdict()
defaultdict is a special type of dictionary from the collections module that automatically gives a default value to a key that doesn’t exist. Useful for incrementing values when the key might not exist.
Python
from collections import defaultdict
d = defaultdict(int)
print(dict(d))
d['best'] += 3
print(dict(d))
Explanation: Initializes a defaultdict with 0 then increments the 'best' key by 3.
Using get()
get() method is used to safely access a value from a dictionary. If the key doesn’t exist, it returns a default value (like 0), which is useful when incrementing values without causing an error.
Python
d = {'gfg': 1, 'is': 2, 'for': 4, 'CS': 5}
print(d)
d['best'] = d.get('best', 0) + 3
print(d)
Output{'gfg': 1, 'is': 2, 'for': 4, 'CS': 5}
{'gfg': 1, 'is': 2, 'for': 4, 'CS': 5, 'best': 3}
Explanation: increments the value of the 'best' key by 3, initializing it to 0 if it doesn't exist.
Using setdefault()
setdefault() method is a built-in dictionary method that sets the default value for a key if it is not already present in the dictionary. If the key is present, it returns the value of that key.
Python
d = {'gfg': 1, 'is': 2, 'for': 4, 'CS': 5}
print(d)
d.setdefault('best', 0)
d['best'] += 3
print(d)
Output{'gfg': 1, 'is': 2, 'for': 4, 'CS': 5}
{'gfg': 1, 'is': 2, 'for': 4, 'CS': 5, 'best': 3}
Explanation: uses setdefault() to initialize 'best' to 0 if it doesn't exist, then increments its value by 3.
Using update()
update() method is used to add new key-value pairs or modify existing ones in a dictionary. It’s useful for quickly updating values without checking if the key exists.
Python
d = {'gfg' : 1, 'is' : 2, 'for' : 4, 'CS' : 5}
print(d)
d.update({'best': d.get('best', 0) + 3})
print(d)
Output{'gfg': 1, 'is': 2, 'for': 4, 'CS': 5}
{'gfg': 1, 'is': 2, 'for': 4, 'CS': 5, 'best': 3}
Explanation: increments the value of the 'best' key by 3 (or sets it to 3 if it doesn't exist) using get() and update().
Using try-except
try-except is a simple way to handle cases where a key might not exist in a dictionary. It tries to increment the key’s value, and if the key doesn’t exist (causing a KeyError), it adds the key with an initial value.
Python
d = {'gfg': 1, 'is': 2, 'for': 4, 'CS': 5}
print(d)
k = 'best'
v = 3
try:
d[k] += v
except KeyError:
d[k] = v
print(d)
Output{'gfg': 1, 'is': 2, 'for': 4, 'CS': 5}
{'gfg': 1, 'is': 2, 'for': 4, 'CS': 5, 'best': 3}
Explanation:
- d is initialized and the key 'best' is incremented by v = 3.
- If 'best' doesn't exist, the KeyError is caught and 'best': 3 is added to the dictionary.
Related Articles:
Similar Reads
Python | Increment value in dictionary In Python, dictionaries store data as keyâvalue pairs. If a key already exists, its value can be updated or incremented. This is commonly used for counting occurrences, like word frequency or item counts. Let's discuss certain ways in which this task can be performed. Using defaultdict()defaultdict
3 min read
Python - Incremental value initialization in Dictionary The interconversion between the datatypes is very popular and hence many articles have been written to demonstrate different kind of problems with their solutions. This article deals with yet another similar type problem of converting a list to dictionary, with values as the index incremental with K
5 min read
Python Iterate Dictionary Key, Value In Python, a Dictionary is a data structure that stores the data in the form of key-value pairs. It is a mutable (which means once created we modify or update its value later on) and unordered data structure in Python. There is a thing to keep in mind while creating a dictionary every key in the dic
3 min read
Get Index of Values in Python Dictionary Dictionary values are lists and we might need to determine the position (or index) of each element within those lists. Since dictionaries themselves are unordered (prior to Python 3.7) or ordered based on insertion order (in Python 3.7+), the concept of "index" applies to the valuesâspecifically whe
3 min read
Python - Print dictionary of list values In this article, we will explore various ways on How to Print Dictionary in Python of list values. A dictionary of list values means a dictionary contains values as a list of dictionaries in Python. Example: {'key1': [{'key1': value,......,'key n': value}........{'key1': value,......,'key n': value}
4 min read
Key Index in Dictionary - Python We are given a dictionary and a specific key, our task is to find the index of this key when the dictionaryâs keys are considered in order. For example, in {'a': 10, 'b': 20, 'c': 30}, the index of 'b' is 1.Using dictionary comprehension and get()This method builds a dictionary using dictionary comp
2 min read