how fast is Python code - another detail

Guillermo Fernandez Castellanos guillermo.fernandez at epfl.ch
Fri Mar 5 10:22:19 CET 2004


Skip Montanaro wrote:
>     >>> if sig not in d.keys():
> vs.
>     >> if sig not in d.iterkeys():
> vs.
>     if sig not in d:
> The first must build the list of keys then search it sequentially.  The
> second doesn't have to build the entire list, but still searches
> sequentially.  The third effectively tries d[sig] and catches the KeyError
> exception of sig is not a key in d.  The third is O(1) in the general case.
> The others are O(len(d)).
Are all this also true for 'for' loops?

G.



More information about the Python-list mailing list