Way for see if dict has a key

Fredrik Lundh fredrik at pythonware.com
Fri Jun 30 06:29:58 EDT 2006


Michele Petrazzo wrote:

> what the preferred way for see if the dict has a key?
> We have a lot of solutions:
>
> key in dict

new syntax (2.3 and later).

> key in dict.keys()

inefficient and pointless.

> dict.has_key(key)

old syntax; use for code that needs to be backwards compatible.

> but what the better or the more "pythonic"?

see above.

</F> 






More information about the Python-list mailing list