13

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?

asked May 28, 2011 at 21:28
6
  • 9
    Example code always helps us help you better. Commented May 28, 2011 at 21:30
  • sort and compare against a known src, springs to mind, but as the others have said, post some code so that we can se how your list looks like! Commented May 28, 2011 at 21:34
  • you do not state what your goal is with the function; e.g. if the function needs to do something with intermediate state Commented May 28, 2011 at 21:45
  • take a look at this stackoverflow.com/questions/6009589/… Commented May 28, 2011 at 21:59
  • -1 ... posting a vague rambling question about code but not showing the code, and not hanging about to answer the inevitable questions. Commented May 29, 2011 at 0:44

4 Answers 4

14

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 all operator:

>>> all([1,2,3,4])
True
>>> all([1,2,3,False])
False

You could also check for the length of the filtered list:

>>> input = [1,2,3,4]
>>> tested = [i for i in input if i > 2]
>>> len(tested) == len(input)
False

If you are using a for construct you can exit the loop early if you come across negative test:

>>> def test(input):
... for i in input:
... if not i > 2:
... return False
... do_something_with_i(i)
... return True

The test function 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.

answered May 28, 2011 at 21:36
5

Maybe you could try with an for ... else statement.

for item in my_list:
 if not my_condition(item):
 break # one item didn't complete the condition, get out of this loop
else:
 # here we are if all items respect the condition
 do_the_stuff(my_list)
answered May 28, 2011 at 21:57
0

You need to loop through your whole list and check the condition before trying to do anything else with the data, so you need two loops (or use some built in that does the loop for you, like all()). From this codepad with nothing too fancy, http://codepad.org/pKfT4Gdc

def my_condition(v):
 return v % 2 == 0
def do_if_pass(l):
 list_okay = True
 for v in l:
 if not my_condition(v):
 list_okay = False
 if list_okay:
 print 'everything in list is okay, including',
 for v in l:
 print v,
 print
 else:
 print 'not okay'
do_if_pass([1,2,3])
do_if_pass([2,4,6])
answered May 28, 2011 at 21:37
0

You must always be careful if you're deleting items from your list while you're trying to iterate through it.

If you're not deleting then does this help:

>>> yourlist=list("abcdefg")
>>> value_position_pairs=zip(yourlist,range(len(yourlist)))
>>> value_position_pairs
[('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4), ('f', 5), ('g', 6)]
>>> filterfunc=lambda x:x[0] in "adg"
>>> value_position_pairs=filter(filterfunc,value_position_pairs)
>>> value_position_pairs
[('a', 0), ('d', 3), ('g', 6)]
>>> yourlist[6]
'g'

now if value_position_pairs is empty you're done. If not you can increase i by one to go to the next value or iterate through the failed values using their position in the array.

answered May 28, 2011 at 23:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.