Python program How To Handle Missing Keys in Dictionary.
In this example shows, we will check the input value of users in the dictionary and print which key-value is present or an 'error statement' when nothing is present.
Example.py
# the dictionary using get() method lists = {'Sekar' : 'Head Master' , 'Suresh' : 'Teacher' , 'kumar' : 'Correspondent'} # Getting user input key = input("Enter the key to search :") # Logic to handle missing keys in dictionary print(lists.get(key, "User key is not in Dictionary"))
Output 1
Enter the key to search :kumar Correspondent
Output 2
Enter the key to search :ram User key is not in Dictionary