Way for see if dict has a key

André andre.roberge at gmail.com
Fri Jun 30 13:27:47 CEST 2006


Michele Petrazzo wrote:
> Bruno Desthuilliers wrote:
> >> but what the better
> >
> > Depends on the context.
> >
>
> If know only one context: see if the key are into the dict... What other
> context do you know?
>
> Michele

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

if key in dict 

André




More information about the Python-list mailing list