unintuitive dict timings

Ludovico Magnocavallo ludo at asiatica.org
Sun Oct 12 17:30:03 EDT 2003


Bob van der Poel wrote:

> # Test of various methods to add an item to a list in a dict.
> 
> def ifelse(key, val):
>  if d.has_key(key):
>   d[key].append(val)
>  else:
>   d[key]=[val]

You could also add:

def ifelse2(key, val):
     if key in d:
         d[key].append(val)
     else:
         d[key]=[val]

which on my machine is faster than either get1 or get2, and slightly 
slower than ifelse.

Ludo





More information about the Python-list mailing list