Question

I'm looking for a loop construct like for i in list where i < n. I would like to replace this mess:

for i in list:
    if i < n:
        #logic here

Is there something more compact and more elegant?

Was it helpful?

Solution

I'd write it with a guard condition to avoid the layer of indentation.

for i in list:
    if i >= n: continue

A one-liner is this:

for i in (k for k in list if k < n):
    ...

But I think the obvious code with the if inside the loop is much easier to understand.

OTHER TIPS

You can use itertools.ifilter as follows:

>>> import itertools
>>> inlist = [1,2,3,4,5,6,7]
>>> for i in itertools.ifilter(lambda x: x < 5,inlist):
...     print i
1
2
3
4

If you want the reverse condition, e.g. greater than:

>>> inlist = [1,2,3,4,5,6,7]
>>> for i in itertools.ifilter(lambda x: x > 5, inlist):
...     print i
... 
6
7

Short answer: No.

Long answer:

You could of course do this:

for i in (a for a in lst if a < n):
    # logic

But obviously that's a lot messier than your solution.

for i in [x for x in list if x<n]:
    pass

In python for this kind of things you have to use while, the for loop is iterating the list you just gave it.

It seems that itertools.takewhile would do what you want.

From the mothership:

itertools.takewhile(predicate, iterable)

Make an iterator that returns elements from the iterable as long as the predicate is true. Equivalent to:

def takewhile(predicate, iterable):
    # takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
    for x in iterable:
        if predicate(x):
            yield x
        else:
            break

EDIT !ignore me! as Karl says ifilter(...) is the correct solution.

takewhile is only useful as they say on the mothership:

Make an iterator that returns elements from the iterable as long as the predicate is true.

I'd change that to be

Make an iterator that returns elements from the iterable only as long as the predicate is true.

As Buffy would say:

My bad.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top