1

For map/lambda expression with array a = [1, 2, 3, 4]:

f = map(lambda x : x + 32, a)

Seems I can simplify write as:

f = [x + 32 for x in a]

I am wondering whether there is any difference.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
BlueDolphin
  • 9,765
  • 20
  • 59
  • 74

1 Answers1

2

lambda tends to be slower cause of the function's overhead. Also lambdas tend to make code more difficult to read. You can also time it:

#!/usr/bin/env python

import time

a = [1, 2, 3, 4]

t1 = time.time()
f = map(lambda x : x + 32, a)
t2 = time.time()-t1
print t2

t3 = time.time()
g = [x + 32 for x in a]
t4 = time.time()-t3
print t4

This code returned:

7.86781311035e-06
2.14576721191e-06

Also I tried the same thing for larger lists and the time taken is almost double for lambda expression.

python -m timeit '[x + 32 for x in range(100000)]'

>> 100 loops, best of 3: 6.67 msec per loop

python -m timeit 'map(lambda x : x + 32, range(100000))'

>> 100 loops, best of 3: 12.5 msec per loop

which is huge difference in performance.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
coder
  • 12,832
  • 5
  • 39
  • 53
  • 2
    @KeerthanaPrabhakaran performance is always a big thing to consider. See the metrics above, I like lambdas too but they tend to be slow. – coder Oct 26 '17 at 16:57
  • 2
    @KeerthanaPrabhakaran What is more Pythonic than a list comprehension? And "Readability counts" - a lot! – user2390182 Oct 26 '17 at 17:01
  • 2
    @KeerthanaPrabhakaran but a lambda doesn't divide code into functions, it *uses an anonymous function*. – juanpa.arrivillaga Oct 26 '17 at 17:04
  • @frank this isn't a very good timing comparison. You should really use `timeit`, but more importantly, you are using a very short list, so the majority of your difference is really just the differing overheads of the two approaches. – juanpa.arrivillaga Oct 26 '17 at 17:05
  • @Miraj50 thank you very much! – coder Oct 26 '17 at 17:09
  • 2
    Is it slow ? My test showed otherwise [prateek@localhost pythonista]$ python -mtimeit -s'a=[1,2,3,4]' 'map(lambda x:x+2,a)' 1000000 loops, best of 3: 0.189 usec per loop [prateek@localhost pythonista]$ python -mtimeit -s'a=[1,2,3,4]' '[x + 32 for x in a]' 1000000 loops, best of 3: 0.291 usec per loop [prateek@localhost pythonista] – The_Lost_Avatar Oct 26 '17 at 17:09
  • I get it. I get it. But Lambda is nothing but anonymous functions in python! And as far as the performance is concerned, try running the same code in a for loop. There using map and lambda seems better. – Keerthana Prabhakaran Oct 26 '17 at 17:16
  • 1
    @KeerthanaPrabhakaran One can ease one's life (and that of one's co-coders) living by simple rules. Not cluttering a codebase with `lambda`-contaminated `map` and `filter` expressions is one of them ;) – user2390182 Oct 26 '17 at 17:18
  • True. For simpler cases, a simple list comprehension may be more than enough. But what I meant was programming in a bigger scale where performance really matters. – Keerthana Prabhakaran Oct 26 '17 at 17:21
  • 1
    @KeerthanaPrabhakaran in my own tests and in the tests you see linked around here tend to show that list-comprehensions are faster, with the exception of mapping built-in functions, e.g. `list(map(float, seq_of_int))` – juanpa.arrivillaga Oct 26 '17 at 17:36
  • @The_Lost_Avatar are you on Python 2? On Python 3, you need `list(map(...))`, anyway, those tests are nearly worthless! `a` has length 4! – juanpa.arrivillaga Oct 26 '17 at 17:39
  • I've tested using both python2 and python3! And, list comprehensions are faster only when the code needs to be executed a few number of times. – Keerthana Prabhakaran Oct 26 '17 at 17:46