Python Check if key exists in dictionary
By:Roy.LiuLast updated:2019-08-17
In Python, you can use the in operator to check if a key exists in a dictionary.
test.py
def main():
fruits = {
'apple':1,
'orange':2,
'banana':3
#if key 'apple' exists in fruits?
if 'apple' in fruits:
print(fruits['apple'])
if __name__ == '__main__':
main()
Output
P.S Tested with Python 3.4.3
Note
has_key() is deprecated in favor of key in d.
has_key() is deprecated in favor of key in d.
References
From:一号门
Previous:How to check Debian version

COMMENTS