Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchPC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11For an existing key, use d[key] += amount. If the key might be missing and should start at zero, use d[key] = d.get(key, 0) + amount. For repeated accumulation, defaultdict(int) or Counter can avoid a separate existence check.
Increment a value when the key already exists
Use the key to read its current value, add the amount, and assign the result back to that key:
d = {"apples": 4}
d["apples"] += 1
print(d) # {'apples': 5}
This is shorthand for d["apples"] = d["apples"] + 1. It assumes the key is present: looking up a missing key in a regular dictionary raises KeyError. See the Python built-in types documentation.
Handle a key that may be missing
For an occasional update to a regular dictionary, use get to supply the starting value, then assign the sum:
#1 Best Overall
d = {"apples": 4}
key = "oranges"
amount = 1
d[key] = d.get(key, 0) + amount
print(d) # {'apples': 4, 'oranges': 1}
d.get(key, 0) returns the value for key when it exists, or 0 when it does not. The assignment is what stores the updated result in the dictionary. Choose a different default if zero is not the appropriate starting value for your data.
Choose a pattern for repeated updates
Use defaultdict(int) for ongoing accumulation
When many keys may appear over repeated updates, collections.defaultdict can create a zero count the first time a missing key is accessed with square brackets:
Rank #2
from collections import defaultdict
counts = defaultdict(int)
counts["apple"] += 1
counts["apple"] += 1
print(counts["apple"]) # 2
The factory int returns zero. On a missing-key square-bracket lookup, defaultdict calls that factory, inserts the resulting value, and returns it. This is the pattern shown in the Python collections documentation.
Use Counter when the task is counting occurrences
collections.Counter is designed for counting hashable objects. It can count an iterable initially, and a missing item reads as zero, so incrementing it is straightforward:
Recommended Free Tools
from collections import Counter
items = ["apple", "pear", "apple"]
counts = Counter(items)
counts["apple"] += 1
print(counts["apple"]) # 3
A Counter can contain zero or negative counts; reaching zero does not automatically remove an entry. See the Counter reference.
When to use setdefault
setdefault(key, value) inserts and returns value only when the key is absent; if the key exists, it returns the existing value. It can be used for an increment:
d[key] = d.setdefault(key, 0) + amount
For numeric updates, get plus assignment or defaultdict(int) usually makes the intent clearer. setdefault by itself initializes a missing key; it does not increment an existing value. Its behavior is documented in the Python collections examples.
Quick Recap
Best Value
Common pitfalls
- Forgetting the assignment: adding an amount to a value does not update the dictionary unless the result is assigned to its key.
- Assuming a missing regular-dict key starts at zero:
d[key] += 1raisesKeyErrorif the key is absent. - Assuming
defaultdict.get()creates an entry: itsget()method behaves like a normal dictionary’s. It does not call the default factory;d.get(key)returnsNonewhen the key is absent unless you supply another default. - Using zero when it is not the right default: if values may be
None, or should begin from something other than zero, select a default that fits the data.
Which approach should you use?
| Situation | Use |
|---|---|
| The key is guaranteed to exist | d[key] += amount |
| The key may be absent, with only occasional updates to a regular dictionary | d[key] = d.get(key, 0) + amount |
| You repeatedly accumulate values under keys that may be absent | defaultdict(int) |
| You are counting occurrences of hashable items | Counter |
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →




