Dictionaries and incrementing keys

Steve Crook steve at mixmin.net
Tue Jun 14 06:57:44 EDT 2011


Hi all,

I've always done key creation/incrementation using:

if key in dict:
    dict[key] += 1
else:
    dict[key] = 1

Today I spotted an alternative:

dict[key] = dict.get(key, 0) + 1

Whilst certainly more compact, I'd be interested in views on how
pythonesque this method is.



More information about the Python-list mailing list