someDict = {'foo': True}
if 'foo' in someDict and someDict['foo']:
print 'success'
Following code works fine. I’m just wondering if there is a better/shorter way of checking if key exists and its value is true.
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.
someDict.get('foo')This will return None if
foois not insomeDict, otherwise it will return the value found. You can optionally pass a second argument which will be the value returned if it does not exist.