Given a dictionary in Python, write a Python program to check whether a given key already exists in a dictionary. If present, print “Present” and the value of the key. Otherwise print “Not present”.
Examples:
Input : {'a': 100, 'b':200, 'c':300}, key = b
Output : Present, value = 200
Input : {'x': 25, 'y':18, 'z':45}, key = w
Output : Not present
Approach #1 : Using Inbuilt method keys()
keys() method returns a list of all the available keys in the dictionary. With the Inbuilt method keys(), use if statement and the ‘in’ operator to check if the key is present in the dictionary or not.
# Python3 Program to check whether a # given key already exists in a dictionary. # Function to print sum def checkKey(dict, key): if key in dict.keys(): print("Present, ", end =" ") print("value =", dict[key]) else: print("Not present") # Driver Code dict = {'a': 100, 'b':200, 'c':300} key = 'b'checkKey(dict, key) key = 'w'checkKey(dict, key) |
Output:
Present, value = 200 Not present
Approach #2 : Using if and in
This method simply uses if statement to check whether the given key exist in the dictionary.
# Python3 Program to check whether a # given key already exists in a dictionary. # Function to print sum def checkKey(dict, key): if key in dict: print("Present, ", end =" ") print("value =", dict[key]) else: print("Not present") # Driver Code dict = {'a': 100, 'b':200, 'c':300} key = 'b'checkKey(dict, key) key = 'w'checkKey(dict, key) |
Output:
Present, value = 200 Not present
Approach #3 : Using Inbuilt method has_key()
has_key() method returns true if a given key is available in the dictionary, otherwise it returns a false. With the Inbuilt method has_key(), use if statement to check if the key is present in the dictionary or not.
Note – has_keys() method has been removed from Python3 version. Therefore, it can be used in Python2 only.
# Python3 Program to check whether a # given key already exists in a dictionary. # Function to print sum def checkKey(dict, key): if dict.has_key(key): print "Present, value =", dict[key] else: print "Not present" # Driver Function dict = {'a': 100, 'b':200, 'c':300} key = 'b'checkKey(dict, key) key = 'w'checkKey(dict, key) |
Output:
Present, value = 200 Not present
Recommended Posts:
- Python | Check if tuple exists as dictionary key
- Python | Test if key exists in tuple keys dictionary
- Python: Check if a File or Directory Exists
- Python - Check if a file or directory exists
- Check if element exists in list in Python
- Python | Check if key has Non-None value in dictionary
- Python | Check if element exists in list of lists
- Check if a value exists in a DataFrame using in & not in operator in Python-Pandas
- Python - Check if all values are K in dictionary
- Python | Check if dictionary is empty
- Python | Check if all values are 0 in dictionary
- Python | Check if one dictionary is subset of other
- Python | Check for None values in given dictionary
- Python - Check for Key in Dictionary Value list
- Python dictionary, set and counter to check if frequencies can become same
- Python | Check if given multiple keys exist in a dictionary
- Python Dictionary | Check if binary representations of two numbers are anagram
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python | Check if a list exists in given list of lists
- Python | Convert flattened dictionary into nested 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.

