Python | Dictionary has_key()
Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key : value pair.
In Python Dictionary, has_key() method returns true if specified key is present in the dictionary, else returns false.
Syntax: dict.has_key(key)
Parameters:
- key – This is the Key to be searched in the dictionary.
Returns: Method returns true if a given key is available in the dictionary, otherwise it returns a false.
Example #1:
# Python program to show working # of has_key() method in Dictionary # Dictionary with three items Dictionary1 = { 'A': 'Geeks', 'B': 'For', 'C': 'Geeks' } # Dictionary to be checked print("Dictionary to be checked: ") print(Dictionary1) # Use of has_key() to check # for presence of a key in Dictionary print(Dictionary1.has_key('A')) print(Dictionary1.has_key('For')) |
Output:
Dictionary to be checked:
{'A': 'Geeks', 'C': 'Geeks', 'B': 'For'}
True
False
Example #2:
# Python program to show working # of has_key() method in Dictionary # Dictionary with three items Dictionary2 = { 1: 'Welcome', 2: 'To', 3: 'Geeks' } # Dictionary to be checked print("Dictionary to be checked: ") print(Dictionary2) # Use of has_key() to check # for presence of a key in Dictionary print(Dictionary2.has_key(1)) print(Dictionary2.has_key('To')) |
Output:
Dictionary to be checked:
{1: 'Welcome', 2: 'To', 3: 'Geeks'}
True
False
Recommended Posts:
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert string dictionary to dictionary
- Python Dictionary
- Python | Get key from value in Dictionary
- Python | Nested Dictionary
- Python | Add new keys to a dictionary
- Few mistakes when using Python dictionary
- Python Dictionary Comprehension
- Python | Key index in Dictionary
- Python | Convert a set into dictionary
- Python | Transcribing dictionary key
- Python | Accessing Key-value in Dictionary
- Python Dictionary copy()
- Python | Get key with maximum value in Dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



