7

I have a couple of lists which vary in length, and I would like to compare each of their items with an integer, and if any one of the items is above said integer, it breaks the for loop that it is in.

for list in listoflists:
    if {anyiteminlist} > 70:
        continue    #as in skip to next list

    {rest of code here} 

Basically, I need to say: "If anything in this list is above 70, continue the loop with the next list"

3
  • What is the reason not to use a nested for loop? Any internal method that list has will still use an O(n) search to find out if any element is greater than some number (70). If you really need to avoid a nested O(n) search, use a different/custom data structure.
    – Alan
    Feb 13, 2012 at 2:43
  • Not that it matters, but in your pseudocode you write "if {anyiteminlist} > 70: continue", but you write "if anything in this list is below 70, continue the loop with the next list". Am I misreading, or are those almost opposites?
    – DSM
    Feb 13, 2012 at 2:47
  • Sorry about that, I fixed it. No, it's not homework.
    – rptynan
    Feb 13, 2012 at 2:54

6 Answers 6

23

Don't use list as a variable name, it shadows the builtin list(). There is a builtin function called any which is useful here

if any(x>70 for x in the_list):

The part inbetween the ( and ) is called a generator expression

0
3

Well, I'd probably do it using the generator expression, but since no one else has suggested this yet, and it doesn't have an (explicit) nested loop:

>>> lol = [[1,2,3],[4,40],[10,20,30]]
>>> 
>>> for l in lol:
...     if max(l) > 30:
...         continue
...     print l
... 
[1, 2, 3]
[10, 20, 30]
3
  • 4
    max will not short-circuit, but any will - that is, the first element of the list greater than the threshold value will cause any to evaluate to True. max will look at every value to find the maximum value, even if the first element of the list exceeds the threshold and the rest don't really need to be looked at.
    – PaulMcG
    Feb 13, 2012 at 3:35
  • @PaulMcGuire: yep. The only possible advantage to computing the max is that it avoids an explicit loop, and the only reason I can think of to do that is if that's what a question asked for.. (and I don't mean this question here on SO.)
    – DSM
    Feb 13, 2012 at 3:38
  • 1
    I suspect that the "no for loop" condition may have been due to some question over how to continue the outer loop from inside the inner loop. A "continue" in the inner loop would just continue the inner loop, not the outer loop.
    – PaulMcG
    Feb 13, 2012 at 4:41
1

You can use the built-in function any like this:

for list in listoflists:
    if any(x < 70 for x in list):
        continue

The any function does short-circuit evaluation, so it will return True as soon as an integer in the list is found that meets the condition.

Also, you should not use the variable list, since it is a built-in function.

1

You can shorten it to this :D

for good_list in filter(lambda x: max(x)<=70, listoflists):
   # do stuff
0

If you're using python 2.5 or greater, you can use the any() function with list comprehensions.

for list in listoflists:
  if any([i > 70 for i in list]):
    continue
4
  • 6
    You don't need or want the list-comp inside the any: it prevents short-circuiting.
    – DSM
    Feb 13, 2012 at 2:45
  • for i in list still looks like a for loop to me.
    – Alan
    Feb 13, 2012 at 2:45
  • @Alan - its a generator expression that uses for to define the values to be generated. In this form, it is a for loop that is executed in fast C code, not in slow Python code.
    – PaulMcG
    Feb 13, 2012 at 3:38
  • @PaulMcGuire: That's interesting. I did not know that. I was merely pointing out that the original question (oddly) stated no nested for loop. Regardless, even if it's implemented faster, or executed on faster hardware, at some point n^2 will always be slower than Nlog(n)
    – Alan
    Feb 13, 2012 at 4:20
0

Using the builtin any is the clearest way. Alternatively you can nest a for loop and break out of it (one of the few uses of for-else construct).

for lst in listoflists:
    for i in lst:
        if i > 70:
            break
    else:
        # rest of your code
        pass

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.