Menu Close

Check the key in Dictionary Python…FTC

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

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 two dictionaries key-value elements
2. Get the key value to be searched

Output:
1. Display the first dictionary
2. Display the second dictionary
3. Display the concatenated dictionary
4. Display whether the key is present or nor and the respective value of the key.

Refer sample input and output for formatting specification.

INPUT:
81
9
64
8
81

OUTPUT:
First Dictionary: {81: 9}
Second Dictionary: {64: 8}
Concatenated dictionary is  {81: 9, 64: 8}
Key is present and value of the key is 9
d1={}
a=int(input())
b=int(input())
d1[a]=b

d2={}
c=int(input())
d=int(input())
d2[c]=d

print('First Dictionary: ',d1)
print('Second Dictionary: ',d2)
d1.update(d2)
print('Concatenated dictionary is ', d1)
val=int(input("\nEnter Key: "))
c=0
for k,v in d1.items():
    if k==val:
        c=c+1
        print("Key is present and value of the key is ",v)
if c==0:
    print("Key not present")


OUTPUT_1:
81
9
64
8
First Dictionary:  {81: 9}
Second Dictionary:  {64: 8}
Concatenated dictionary is  {81: 9,  64: 8}

Enter Key:  81
Key is present and value of the key is  9


OUTPUT_2:
56
65
25
20
First Dictionary:  {56: 65}
Second Dictionary:  {25: 20}
Concatenated dictionary is  {56: 65,  25: 20}

Enter Key:  25
Key is present and value of the key is  20


OUTPUT_3:
100
101
555
556
First Dictionary:  {100: 101}
Second Dictionary:  {555: 556}
Concatenated dictionary is  {100: 101,  555: 556}

Enter Key:  100
Key is present and value of the key is  101


ILLUSTRATION

Executed using python3