I\'m currently re-engaging with Python after a long absence and loving it. However, I find myself coming across a pattern over and over. I keep thinking that there must be a
Use a defaultdict:
defaultdict
from collections import defaultdict foo = defaultdict(int) foo[bar] += 1
In Python >= 2.7, you also have a separate Counter class for these purposes. For Python 2.5 and 2.6, you can use its backported version.