I have a list that I am looping through with a “for” loop and am running each value in the list through an if statement. My problem is that I am trying to only have the program do something if all the values in the list pass the if statement and if one doesn’t pass, I want it to move along to the next value in the list. Currently it is returning a value if a single item in the list passes the if statement. Any ideas to get me pointed in the right direction?
Share
Python gives you loads of options to deal with such a situation. If you have example code we could narrow that down for you.
One option you could look at is the
alloperator:You could also check for the length of the filtered list:
If you are using a
forconstruct you can exit the loop early if you come across negative test:The
testfunction above will return False on the first value that’s 2 or lower, for example, while it’ll return True only if all values were larger than 2.