0

I have a dict like below:

 s = {1: True, 2: False, 3: False} 

I want to find out if there exists any key in the dict with a value of False.
Is there any way to find out without iterating all the keys of the dict?
Basically, I can do something like if 2 in s to find out if a given key exists in the dict.
Is there anything like it for the value of the keys in the dict?

4
  • No. That is using the dictionary backwards. Commented Aug 20, 2016 at 10:45
  • backwards as in ? Could you kindly clarify ? Commented Aug 20, 2016 at 10:46
  • 1
    Backwards as in you're supposed to look up a key to retrieve its associated value, not the other way around. Commented Aug 20, 2016 at 10:47
  • 1
    The linked question is rather old and the answers are for Python 2. The best way in Python 3 is if False in s.values():. It has to do a linear scan of the values, so it's not as efficient as testing for a dict key, or an element in a set, but it's faster than manually looping over the dict's values. Commented Aug 20, 2016 at 10:57

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.