Search code examples
pythondictionarykey

How do I find if a value exists in a dictionary?


If I have a dictionary:

d = {0:'Fred', 1:'Fred', 2:'Zippy', 3:'Bob'}

how can I find out how many times "Fred" occurs in it?


Solution

  • You can do this as follows:

    c=0
    for i in d.values():
        if i=='Fred':
            c+=1
    print(c)