The lambda
is only evaluated once(converted to a code object), so there's no need of of assigning it to a variable.
>>> import dis
>>> def func():
... map( lambda x: len(x), aLotOfData)
...
>>> dis.dis(func)
2 0 LOAD_GLOBAL 0 (map)
3 LOAD_CONST 1 (<code object <lambda> at 0x31c3d30, file "<ipython-input-30-27b0b12b0965>", line 2>)
6 MAKE_FUNCTION 0
9 LOAD_GLOBAL 1 (aLotOfData)
12 CALL_FUNCTION 2
15 POP_TOP
16 LOAD_CONST 0 (None)
19 RETURN_VALUE
But as map
is slow with lambda's so should use a list comprehension or generator expression(if you want an iterator) here.
[len(x) for x in aLotOfData]
or define a full function, which is more readable:
def my_func(x):
#do something with x
return something
[my_func(x) for x in aLotOfData]
Which is more preferable to use in Python: lambda functions or nested functions ('def')?