Open In App

Increment value in dictionary - Python

Last Updated : 27 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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))

Output
{}
{'best': 3}

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:


Practice Tags :

Similar Reads