basic question, sorry
Just van Rossum
just at letterror.com
Mon Aug 21 05:54:40 EDT 2000
Will Ware wrote:
>
> Ruud de Rooij (*@spam.ruud.org) wrote:
> > dict.has_key(n)
>
> Alternatively:
>
> if n in dict.keys():
> do stuff
No!! That's a linear search through the keys list. Additionally, this
key list must be created first. So this is *always* slower.
A valid alternative is:
try:
x = dict[n]
except KeyError:
..key not available..
else:
..key is available..
If you expect the key to usually be available, the try/except approach
may be faster, but in general it's better to use has_key().
Just
More information about the Python-list
mailing list