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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
inoperator, but you could just as easily sayif you prefer that idiom, but the
has_keymethod has been deprecated in Python 3.x, so you should probably avoid it.