lambda in list comprehension acting funny

Steven D'Aprano steve+comp.lang.python at
Thu Jul 12 05:59:55 CEST 2012

On Wed, 11 Jul 2012 08:41:57 +0200, Daniel Fetchinson wrote:

> funcs = [ lambda x: x**i for i in range( 5 ) ]

Here's another solution:

from functools import partial
funcs = [partial(lambda i, x: x**i, i) for i in range(5)]

Notice that the arguments i and x are defined in the opposite order. 
That's because partial only applies positional arguments from the left. 
If there was a "right partial" that applies from the right, we could use 
the built-in instead:

funcs = [rpartial(pow, i) for i in range(5)]


More information about the Python-list mailing list