How to Check if a Key Exists in a Python Dictionary?


To check if a key exists in a python dictionary, use the in operator:

d = {"k": True}
if "k" in d:
    print("YES") # Will print YES

if "j" in d:
    print("YES") # Will not execute

Latest Questions

python How to Fix ""zsh: command not found: python" Error on MacOS X? python How to Fix "xlrd.biffh.XLRDError: Excel xlsx file; not supported" Error in Pandas? python How to Remove All Whitespace From a Python String?