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?

dhS
  • 3,739
  • 5
  • 26
  • 55
Agniswar Bakshi
  • 328
  • 5
  • 21
  • No. That is using the dictionary backwards. – TigerhawkT3 Aug 20 '16 at 10:45
  • backwards as in ? Could you kindly clarify ? – Agniswar Bakshi Aug 20 '16 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. – TigerhawkT3 Aug 20 '16 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. – PM 2Ring Aug 20 '16 at 10:57

0 Answers0