Pythonic way for missing dict keys
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Thu Jul 19 00:33:26 EDT 2007
Alex Popescu a écrit :
> Jakub Stolarski <jakub.stolarski at gmail.com> wrote in
> news:1184961448.456134.153020 at k79g2000hse.googlegroups.com:
>
>
>>Version 1 and 2 do different thing than version 3. The latter doesn't
>>add value to dict.
>>
>>As it was mentioned before, use:
>>1 - if you expect that there's no key in dict
>>2 - if you expect that there is key in dict
>>
>
>
> I may be missing something
You are.
> but I think the 3 approaches are completely
> equivalent in terms of functionality.
d = dict()
answer = d.get('answer', 42)
answer in d
=> False
More information about the Python-list
mailing list