CBSE Class 11 Informatics Practices Question 57 of 66

Dictionary — Question 29

Back to all questions
29
Question

Question 18

Write a Python program that accepts a value and checks whether the inputted value is part of the given dictionary or not. If it is present, check for the frequency of its occurrence and print the corresponding key, otherwise print an error message.

Solution
d = {
    'A': 'apple',
    'B': 'banana',
    'C': 'apple',
    'D': 'orange',
    'E': 'strawberry'
}

v = input("Enter a value to check: ")
k = []

for key, value in d.items():
    if value == v:
        k.append(key)

if len(k) > 0:
    print("The value", v, "is present in the dictionary.")
    print("It occurs", len(k), "time(s) and is associated with the following key(s):", k)
else:
    print("The value", v, "is not present in the dictionary.")
Output
Enter a value to check: apple
The value apple is present in the dictionary.
It occurs 2 time(s) and is associated with the following key(s): ['A', 'C']
Answer