Looping over selected items

Suppose you wanted to loop through a list of strings and print all the strings that are more than 3 characters long. You could do it like this:

values = ['a', 'bcd', 'efgh', 'pqrst', 'yz']

def longer_than_3(s):
    return len(s) > 3

for v in values:
    if longer_than_3(v):
        print(v)
        do_other_stuff()

The longer_than_3 function returns true if the string is longer than 3 characters, or false otherwise. If it is true, we print the string, and maybe do some other stuff. If it is false, we skip the entire body of the loop.

This code is fine as it is, but we could improve it slightly using the filter function.

Using filter

The filter function accepts a function object, and a sequence of values. It applies the function to each value in the sequence, and lets those values for which the function return true.

So how do we use filter in a loop? Well, much the same as reversed or any of the other loop functions, like this:

values = ['a', 'bcd', 'efgh', 'pqrst', 'yz']

def longer_than_3(s):
    return len(s) > 3

for v in filter(longer_than_3, values):
    print(v)
    do_other_stuff()

The advantage of using filter

Clearly, our code is now one line shorter. But the real advantage, as ever, is that it makes the intent of the code clearer.

The filter function makes it totally clear that the filtering applies to the whole loop. It brings the filter functionality out of the body of the loop and places it directly in for statement itself.

On the other hand, the if statement (in the original code at the start of the article) is a little more ambiguous. You need to inspect the loop before deciding that the condition affects the entire loop body, rather than just part of it. This isn’t too hard with a simple two line loop body, but it is less obvious in complex code.

Using a lambda function

In this case, the function we are using is only a single line of code, so we can use a lambda function instead of a function declaration. This definition creates an unnamed lambda function equivalent to longer_than_3:

lambda x: len(x) > 3

Here is how we use it in the code (notice that the longer_than_3 function is no longer required):

values = ['a', 'bcd', 'efgh', 'pqrst', 'yz']

for v in filter(lambda x: len(x) > 3, values):
    print(v)
    do_other_stuff()

In summary, if you are writing a loop that only processes certain elements within the sequence, consider using a filter function in the for loop, rather than an if statement in the body of the loop, to make the intent of the code clear.

If the selection criterion is a simple, one line function, consider using a lambda function for code brevity and readability.