Menu Close

Write a program to check whether the given key is present in the dictionary….Python FTC

If the key is present then display the value of the entered key and if the key is not present then display the appropriate message

Input:
1. Get size of the dictionary
2. Get the key-value elements
3. Get the key to be searched

Output:
1. Display the dictionary
2. Print either True or False

INPUT
Size of dictionary:  2
25
20
1252
1120

Key:  20


OUTPUT
The dictionary is {25: 1252, 20: 1120}
True
n=int(input("Size of dictionary: "))
d={}
kl=[]
vl=[]
for i in range(n):
    k=int(input())
    kl.append(k)
for i in range(n):
    k=int(input())
    vl.append(k)
for i in range(n):
    d[kl[i]]=vl[i]
print("The dictionary is ",d)
search=int(input("\nKey to be Searched: "))
c=0
for k,v in d.items():
    if search==k:
        c=c+1
        print("True")
if c==0:
    print("False")

OUTPUT:
Size of dictionary: 2
25
20
1252
1120
The dictionary is  {25: 1252, 20: 1120}

Key to be Searched:  20
True


OUTPUT:
Size of dictionary: 2
50
60
200
2000
The dictionary is  {50: 200, 60: 2000}

Key to be Searched:  25
False


ILLUSTRATION

Executed using python 3 Linux Terminal