help make it faster please

Sybren Stuvel sybrenUSE at
Sat Nov 12 10:46:53 CET 2005

Bengt Richter enlightened us with:
> I suspect it's not possible to get '' in the list from
> somestring.split()

Time to adjust your suspicions:

>>> ';abc;'.split(';')
['', 'abc', '']

>>                    countDict[w] += 1
>>                else:
>>                    countDict[w] = 1
> does that beat the try and get versions? I.e., (untested)
>                  try: countDict[w] += 1
>                  except KeyError: countDict[w] = 1

OPs way is faster. A 'try' block is very fast, but trowing & handling
an exception is slow.

>                  countDict[w] = countDict.get(w, 0) + 1

I think this has the potential of being faster than OPs method,
because it's likely to be fully implemented in C.

The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself? 
                                             Frank Zappa

More information about the Python-list mailing list