Append a new value to dict

slais-www slais-www at ucl.ac.uk
Thu Oct 23 20:38:55 CEST 2008


bearophileHUGS at lycos.com wrote:
> Marc 'BlackJack' Rintsch:
>> counter['B'] = counter.get('B', 0) + 1
> 
> If you benchmark it, you will find that using the get() method it's
> quite slower.

Slower than

if 'B' in counter:
 >     counter['B'] += 1
 > else:
 >     counter['B'] = 1

?

It is not slower than defaultdict which I have compared to
 >> counter['B'] = counter.get('B', 0) + 1
on a file with 125,000 additions default dict was significantly slower 
(only ~40seconds v ~30 secs for get) and used twice the memory.

-- 
djc



More information about the Python-list mailing list