How to check if keys exists in dictionary python?
A dictionary is a set of unordered key, value pairs. In a dictionary, The keys must be unique, and they are stored in an unordered manner.
Here we’ll see how to check if the key exists in the dictionary.
e.g.,
Dict = {'John':78, 'zoya':96, 'vicky':65, 'Sonia':85} key = input() try: Dict[key] except KeyError: print("Key doesn't exist!") else: print("Key present!")
e.g.,
Dict = {'John':78, 'zoya':96, 'vicky':65, 'Sonia':85} key = input() if key in Dict: print("Found!") else: print("Not Found!")
This is how we can check the key of the dictionary in python.
Subscribe
Login
Please login to comment
0 Discussion