Python Dictionary has_key() Method
Description
The Python Dictionary has_key() function is used to determine whether a key exists in the dictionary. If the key is in the dictionary dict, it returns true; otherwise, it returns false.
Note:Python 3.X does not support this method.
Syntax
has_key() method syntax:
dict.has_key(key)
Parameters
- key -- The key to look up in the dictionary.
Return Value
Returns true if the key is in the dictionary, otherwise returns false.
Example
The following example demonstrates how to use the has_key() function:
Example
#!/usr/bin/python
tinydict = {'Name': 'Zara', 'Age': 7}
print "Value : %s" % tinydict.has_key('Age')
print "Value : %s" % tinydict.has_key('Sex')
tinydict = {'Name': 'Zara', 'Age': 7}
print "Value : %s" % tinydict.has_key('Age')
print "Value : %s" % tinydict.has_key('Sex')
The output of the above example is:
Value : True Value : FalseOther Extensions
Python Dictionary