Home > Enterprise >  Combine list of lambdas in a single one
Combine list of lambdas in a single one

Time:06-28

What's the pythonic way to combine a list of lambdas in a single function? For example:

lambdas = [lambda x, k=k: x k for k in range(3)]

I would like to get this all in a single lambda similar to this but without having to type it out:

f = lambda x: lambdas[2](lambdas[1](lambdas[0](x)))

CodePudding user response:

You can do this with functools.reduce like below:

from functools import reduce
lambdas = [lambda x, k=k: x k for k in range(3)]
# x = 0
reduce(lambda x, l: l(x), lambdas, x)
# -> l[2](l[1](l[0](x)))
# step_1 : x = x , l = lambda[0] -> lambda[0](x)
# step_2 : x = lambda[0](x), l = lambda[1] -> lambda[1](lambda[0](x))
# step_3 : x = lambda[1](lambda[0](x)), l = lambda[2] -> lambda[2](lambda[1](lambda[0](x)))

CodePudding user response:

The reduce function is defined to be exactly what you want. An alternative is to use a simple for loop.

def f(x):
    for func in lambdas:
         x = func(x)
    return x

CodePudding user response:

to do this with a lambda seems kind of weird.

Is there any specific reason why we cannot:

def function_chainer(lambdas):
    def chained(x):
        for function in lambdas:
            x = function(x)
    return chained

This solution is not a one-liner, but it is pythonic I believe.

If you really need a one-liner, you can use functools.reduce:

lambda x: functools.reduce(lambda a, f: f(a), lambdas, x)

The first argument to reduce governs the way of applying each subsequent element, the second is the iterable (here - our iterable of lambdas) and the last one is the initializer - the first value we want to pass to those lambda functions.

  • Related