Way for see if dict has a key

Fredrik Lundh fredrik at pythonware.com
Fri Jun 30 07:34:37 EDT 2006


"André" wrote:

> Perhaps Bruno meant this:
>
> try:
>    ... my_dict[key] ...
> except:
> ...
>
> when we expect that the key will most often be in my_dict so that
>exception will be raised rarely, otherwise use

on my machine, "key in dict" is about twice as fast as the full try/getitem con-
struct when the key is present in the dict, so that's not a very good optimization.

now, if the OP had been interested in the associated value, things might have
been a bit different.

</F> 






More information about the Python-list mailing list