KnowledgeBoat Logo
LoginJOIN NOW

Informatics Practices

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.

Python Dictionaries

3 Likes

Answer

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']

Answered By

1 Like


Related Questions