How to check multiple dictionaries for values?

If I have:

d = {'a': 1, 'b': 3}
e = {'a': 2, 'b': 6}
f = {'a': 1, 'b': 4}

How would I check that values of the 'b' key in all dictionaries is above 2 and then execute a function?

I have tried:

dicts = [d, e, f]
for i in dicts:
    if i['b'] >= 3:
        func()

However, this calls the function 3 times, whereas I only want to call it once all arguments are met.

>Solution :

dicts = [d, e, f]    
if all([i['b'] >= 3 for i in dicts]):
    func()

Leave a Reply