Single line of code to check for a key in a 2D nested inner dictionary
python
Solution
if k2 in d.get(k1, {}):
# do something
The above fragment is nice if you don't care about whether k1 actually exists or not and merely want to know whether k2 exists inside of it if it does exist. As you can see from my code snippet, I prefer the `in` operator, but you could just as easily say
if d.get(k1, {}).has_key(k2):
# do something
if you prefer that idiom, but the `has_key` method has been deprecated in Python 3.x, so you should probably avoid it.
Problem
Is there a single line method to check whether a Python 2d dict has an inner key/value? Right now i do somethng like this: ``` if d.has_key(k1): if d[k1].has_key(k2): # do something ``` Is there a better way to do this? Thanks