Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map list by partial function vs lambda

Tags:

python

I was wondering whether for most examples it is more 'pythonic' to use lambda or the partial function?

For example, I might want to apply imap on some list, like add 3 to every element using:

imap(lambda x : x + 3, my_list)

Or to use partial:

imap(partial(operator.add, 3), my_list)

I realize in this example a loop could probably accomplish it easier, but I'm thinking about more non-trivial examples.

In Haskell, I would easily choose partial application in the above example, but I'm not sure for Python. To me, the lambda seems the the better choice, but I don't know what the prevailing choice is for most python programmers.

like image 750
Zach L Avatar asked Mar 09 '11 01:03

Zach L


People also ask

Can Map function have more than 2 arguments?

We can pass multiple iterable arguments to map() function, in that case, the specified function must have that many arguments. The function will be applied to these iterable elements in parallel. With multiple iterable arguments, the map iterator stops when the shortest iterable is exhausted.

What does partial do Functools?

You can create partial functions in python by using the partial function from the functools library. Partial functions allow one to derive a function with x parameters to a function with fewer parameters and fixed values set for the more limited function.

What does list map ()) do in Python?

Python's map() is a built-in function that allows you to process and transform all the items in an iterable without using an explicit for loop, a technique commonly known as mapping. map() is useful when you need to apply a transformation function to each item in an iterable and transform them into a new iterable.

How many arguments can map have?

The map function has two arguments (1) a function, and (2) an iterable.


2 Answers

To be truly equivalent to imap, use a generator expression:

(x + 3 for x in mylist)

Like imap, this doesn't immediately construct an entire new list, but instead computes elements of the resulting sequence on-demand (and is thus much more efficient than a list comprehension if you're chaining the result into another iteration).

If you're curious about where partial would be a better option than lambda in the real world, it tends to be when you're dealing with variable numbers of arguments:

>>> from functools import partial
>>> def a(*args):
...     return sum(args)
... 
>>> b = partial(a, 2, 3)
>>> b(6, 7, 8)
26

The equivalent version using lambda would be...

>>> b = lambda *args: a(2, 3, *args)
>>> b(6, 7, 8)
26

which is slightly less concise - but lambda does give you the option of out-of-order application, which partial does not:

>>> def a(x, y, z):
...    return x + y - z
...
>>> b = lambda m, n: a(m, 1, n)
>>> b(2, 5)
-2
like image 158
Amber Avatar answered Nov 12 '22 02:11

Amber


In the given example, lambda seems most appropriate. It's also easier on the eyes.

I have never seen the use of partial functions in the wild.

like image 33
Dimitry Avatar answered Nov 12 '22 01:11

Dimitry