Python Dictionary – has_key() method

It is a method in python that returns True if the key is present in the dictionary, else returns False.

Syntax:
dictionary.has_key(key)

Also Read: Python Dictionary – fromkeys() method

Example 1:

# Python code to understand has_key() method in dictionary
# www.codewindow.in

s = {0:"CodeWindow", 1:"India", 2:"News"}
x = s.has_key(1)
print(x)

Output:

True

Explanation:
Here it returned True since the key (1) is present in the dictionary.

Example 2:

# Python code to understand has_key() method in dictionary
# www.codewindow.in

s = {"site":"CodeWindow", "country":"India", "type":"News", "platform":"IT"}
x = s.has_key("origin")
print(x)

Output:

False

Explanation:
Here it returned False since the key (origin) is absent in the dictionary.


You Missed

Also Checkout