Dictionaries and incrementing keys

Steve Crook steve at mixmin.net
Tue Jun 14 08:57:24 EDT 2011


On Tue, 14 Jun 2011 13:16:47 +0200, Peter Otten wrote in
Message-Id: <it7fu4$rc5$1 at solani.org>:

> Your way is usually faster than
>  
>> dict[key] = dict.get(key, 0) + 1

Thanks Peter, ran it through Timeit and you're right. It's probably also
easier to read the conditional version, even if it is longer.

> You may also consider
>
> http://docs.python.org/library/collections.html#collections.defaultdict
> http://docs.python.org/library/collections.html#collections.Counter

Useful functions, thanks again.



More information about the Python-list mailing list