Python Dictionary has_key() method
Description
The Python Dictionary has_key() function checks whether a key exists in the dictionary, returning true if the key is in dict and false otherwise.
**Note:** Python 3.x does not support this method.
Syntax
has_key() method syntax:
dict.has_key(key)
Parameters
- key -- the key to look for in the dictionary.
Return Value
Returns true if the key is in the dictionary, otherwise false.
Example
The following example shows how to use the has_key() function:
Example
#!/usr/bin/python
tinydict
=
{
'Name'
:
'Zara'
,
'Age'
:
7
}
"Value : %s"
% tinydict.
has_key
(
'Age'
)
"Value : %s"
% tinydict.
has_key
(
'Sex'
)
The output of the above example is:
Value : True
Value : False
Python Dictionary