if not any(myList[0:5]):
does this also work with a dictionary instead of a list?
I want to check if the first five key-value-pairs of my dictionary all have the value ‘False’.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, dictionary keys are not ordered. You can’t say “the first five keys” of a dictionary and mean something well-defined. You can get a list of the keys, in which case they will of course be ordered in that list, but that doesn’t say anything about the “order” in the dictionary itself.
What you can do is check all keys in a list to see if the corresponding value in the dictionary is
False:Notice that just because there is an order to the key-value pairs in the dictionary literal when initializing
the_dict, that doesn’t mean that order somehow remains in the dictionary itself.If your keys are sortable, you can of course sort them manually and then extract the five first: