Latest web development tutorials

Python dictionary (Dictionary) has_key () method

description

Python dictionary (Dictionary) has_key () function is used to determine whether the key exists in the dictionary, if the key is in the dictionary dict returns true, otherwise returns false.

grammar

has_key () method syntax:

dict.has_key(key)

parameter

  • key - you want to find in the dictionary key.

return value

If the key is in the dictionary returns true, otherwise it returns false.

Examples

The following example shows has_key () function to use:

#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7}

print "Value : %s" %  dict.has_key('Age')
print "Value : %s" %  dict.has_key('Sex')

The above example output is:

Value : True
Value : False