Way for see if dict has a key

Michele Petrazzo michele.petrazzo at TOGLIunipex.it
Fri Jun 30 07:32:47 EDT 2006


André wrote:
> 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:
> ....
> 

Yes I see :)

> when we expect that the key will most often be in my_dict so that
> exception will be raised rarely

I didn't thought this because if I think that a key aren't in a dict, I
use dict.get(key, default)

> 
> otherwise use if key in dict 
> 
> André
> 

Thanks to all,
Michele



More information about the Python-list mailing list