Way for see if dict has a key

Bruno Desthuilliers onurb at xiludom.gro
Fri Jun 30 06:59:42 EDT 2006


Michele Petrazzo wrote:
> Hi ng,
> what the preferred way for see if the dict has a key?
> We have a lot of solutions:
> 
> key in dict
> key in dict.keys()
> dict.has_key(key)
> ...

try:
  dict[key]
except KeyError:
  ...
else:
  ...

> but what the better

Depends on the context.

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list