;

Python Program to check if a Given key exists in a Dictionary


Tutorialsrack 05/05/2020 Python

In this Python program, we will learn how to check if a given key exists in a dictionary or not. There are various ways to do this. In this program, we used the python dictionary built-in keys() and get() functions and without using any built-in function. 

Here is the source code of the program to check if a given key exists in a dictionary or not. 

Program 1: Python Program to check if a given key exists in a Dictionary Using keys() method

In this program, we used the python dictionary built-in key() function, and the key() method in Python dictionary returns a view object that displays a list of all the keys in the dictionary.

Python Program to check if a given key exists in a Dictionary Using keys() method
# Python Program to check if a Given key exists in a Dictionary Using keys() method

# Dictionary
myDict = {'a': 'apple', 'b': 'Banana' , 'o': 'Orange', 'g': 'Guava'}

# Print Dictionary
print("Dictionary: ", myDict)

# Take the Input From the User
key = input("Enter the Key You Want to Search for: ")

# Check Whether the Given key exists in a Dictionary or Not
if key in myDict.keys():
    print("\nKey Exists in the Given Dictionary")
    print("Key = ", key, " and Value = ", myDict[key])
else:
    print("\nKey Does not Exist in the Given Dictionary")
Output

Dictionary:  {'a': 'apple', 'b': 'Banana', 'o': 'Orange', 'g': 'Guava'}

Enter the Key You Want to Search for: o

 

Key Exists in the Given Dictionary

Key =  o  and Value =  Orange



Dictionary:  {'a': 'apple', 'b': 'Banana', 'o': 'Orange', 'g': 'Guava'}

Enter the Key You Want to Search for: p

 

Key Does not Exist in the Given Dictionary

Program 2: Another approach to check if the given key is present in a dictionary or not.

Another approach to check if the given key is present in a dictionary or not.
# Another approach to check if the given key is present in a dictionary or not.

# Dictionary
myDict = {'a': 'apple', 'b': 'Banana' , 'o': 'Orange', 'g': 'Guava'}

# Print the Dictionary
print("Dictionary : ", myDict)

key = input("Enter the Key you want to search for: ")

# Check Whether the Given key exists in a Dictionary or Not
if key in myDict:
    print("\nKey Exists in the Given Dictionary")
    print("Key = ", key, " and Value = ", myDict[key])
else:
    print("\nKey Does not Exist in the Given Dictionary")
Output

Dictionary :  {'a': 'apple', 'b': 'Banana', 'o': 'Orange', 'g': 'Guava'}

Enter the Key you want to search for: b

 

Key Exists in the Given Dictionary

Key =  b  and Value =  Banana



Dictionary :  {'a': 'apple', 'b': 'Banana', 'o': 'Orange', 'g': 'Guava'}

Enter the Key you want to search for: t

 

Key Does not Exist in this Dictionary

Program 3: Python Program to check if a given key exists in a Dictionary Using dictionary.get() method

In this program, we used the python dictionary built-in get() function and this method return the value for the given key if present in the dictionary. If not, then it will return None (Note: if get() function is used with only one argument).

Python Program to check if a given key exists in a Dictionary Using dictionary.get() method
# Python Program to check if a Given key exists in a Dictionary
# Using dictionary.get() method

# Dictionary
myDict = {'a': 'apple', 'b': 'Banana' , 'o': 'Orange', 'g': 'Guava'}

# Print the Dictionary
print("Dictionary : ", myDict)

key = input("Enter the Key you want to search for: ")

# Check Whether the Given key exists in a Dictionary or Not
if myDict.get(key) != None:
    print("\nKey Exists in this Dictionary")
    print("Key = ", key, " and Value = ", myDict[key])
else:
    print("\nKey Does not Exist in this Dictionary")
Output

Dictionary:  {'a': 'apple', 'b': 'Banana', 'o': 'Orange', 'g': 'Guava'}

Enter the Key You Want to Search for: u

 

Key Does not Exist in the Given Dictionary



Dictionary :  {'a': 'apple', 'b': 'Banana', 'o': 'Orange', 'g': 'Guava'}

Enter the Key you want to search for: a

 

Key Exists in the Given Dictionary

Key =  a  and Value =  apple


Related Posts



Comments

Recent Posts
Tags