pythoniterationsequencepython-assignment-expression

Sequence of function iterations


Is there a way to abuse assignment expressions or functional tools to generate the sequence x, f(x), f(f(x)), ... in one line?

Here are some contrived examples to demonstrate:

def iter(x, f, lim=10):
    for _ in range(lim):
        yield x
        x = f(x)

iter(1, lambda x: (2*x)%99)

(This makes one extra function call that goes unused. Ideally this is avoided.)

Another weird idea I had is "One-argument accumulate", even uglier. The idea is to use the binary function but ignore the list elements! It's not a good use of accumulate.

from itertools import accumulate
list(accumulate([None]*10, lambda x,y:2*x, initial=1))

Solution

  • You can use a counter in a list comprehension to determine whether to initialize a number with an assignment expression or to aggregate it with your desired function (assumed to be f = lambda x: (2 * x) % 99 here):

    [n := f(n) if i else 1 for i in range(10)]
    

    This returns:

    [1, 2, 4, 8, 16, 32, 64, 29, 58, 17]
    

    Demo: https://ideone.com/Ye25ek