Way for see if dict has a key

Georg Brandl g.brandl-nospam at gmx.net
Fri Jun 30 11:06:38 EDT 2006


Bruno Desthuilliers wrote:
> looping 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?
>>> 
>> 
>> Why do you want to do that ?
>> 
>> if key in dict:
>>   value = dict[key]
>> else:
>>   value = None
>> 
>> could be write:
> 
> value = dict.get(key, None)

value = dict.get(key)

;)

Georg



More information about the Python-list mailing list