If Dict Contains a particular key

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Apr 26 03:07:03 EDT 2007


En Thu, 26 Apr 2007 03:56:17 -0300, John Nagle <nagle at animats.com>  
escribió:

>>>>> On Tue, 2007-04-24 at 18:28 +0100, Robert Rawlins - Think Blue wrote:
>>>>>> I'm Looking to build a quick if/else statement that checks a
>>>>>> dictionary for a key like follows.
>
>      It would be useful if there was some direct way to get the value
> associated with a key, and None if there's not one.

Such wonderful method exists since immemorial ages -at least since Python  
1.5- and its magic name is "get":

py> d = {'a': 1, 'b': 2}
py> print d.get("a")
1
py> print d.get("z")
None

-- 
Gabriel Genellina



More information about the Python-list mailing list