Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if a dictionary contains a specific key? [duplicate]

Tags:

python

What's the cleanest way to test if a dictionary contains a key?

x = {'a' : 1, 'b' : 2}
if (x.contains_key('a')):
    ....
like image 846
ripper234 Avatar asked Mar 15 '11 13:03

ripper234


1 Answers

'a' in x

and a quick search reveals some nice information about it: http://docs.python.org/3/tutorial/datastructures.html#dictionaries

like image 183
cobbal Avatar answered Oct 18 '22 00:10

cobbal