Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
lambda v list comp
#1
So I've learned that lambda v list comp is almost on par with spaces v tabs (silicon valley lol).
I've sort of taken the side of list comp since it does everything lambda does, until I switched. I hear lambda is significantly faster than list comp? So it is easier on resources.

True?
Reply
#2
(Mar-10-2018, 08:56 PM)mepyyeti Wrote: spaces v tabs
there is no such thing as spaces vs. tabs PEP8 is pretty clear on the subject - spaces https://www.python.org/dev/peps/pep-0008...-or-spaces
could you provide a specific example for using lambda vs using comprehension to be clear what we discuss, because they are not the same at all?
EDIT: do you mean map vs list comprehension?
Reply
#3
(Mar-10-2018, 09:35 PM)buran Wrote:
(Mar-10-2018, 08:56 PM)mepyyeti Wrote: spaces v tabs
there is no such thing as spaces vs. tabs PEP8 is pretty clear on the subject - spaces https://www.python.org/dev/peps/pep-0008...-or-spaces
Yeah I was a bit disheartened reading this. Tabs are just easier. Why would you press space 4x?

Quote:could you provide a specific example for using lambda vs using comprehension to be clear what we discuss, because they are not the same at all?
EDIT: do you mean map vs list comprehension?

no. I mean lambda as in
bar = [1,2,3]
foo = list(map(lambda x: x+10, bar))
print(foo)
Specific example: https://www.python-course.eu/lambda.php
Reply
#4
(Mar-11-2018, 09:17 PM)mepyyeti Wrote: Tabs are just easier. Why would you press space 4x?
No one does that. Every modern tool supports configuring the tab key to insert 4 spaces - many if not most have this behavior by default.
Reply
#5
(Mar-10-2018, 08:56 PM)mepyyeti Wrote: I hear lambda is significantly faster than list comp? So it is easier on resources.
Not True in most cases are list comp faster.
λ python -m timeit "list(map(lambda x: x+10, [1,2,3]))"
1000000 loops, best of 3: 1.2 usec per loop

λ python -m timeit "[x+10 for x in [1,2,3]]"
1000000 loops, best of 3: 0.463 usec per loop 
Functional style can have advantages if done right,not talking about speed but less side effect.
Can go down a rabbit hole if talk to people who love functional programming Shifty
Python is of course not a functional language,but has support for a functional programming style.
List comp is looked upon as more Pythonic.

(Mar-11-2018, 09:17 PM)mepyyeti Wrote: Why would you press space 4x?
No one press 4-space,Tab is set to move 4-space in almost all Python editors.
If not configure Tab to move 4-space.
Reply
#6
(Mar-11-2018, 09:17 PM)mepyyeti Wrote: no. I mean lambda as in
actually, it's the map that is alternative to list comprehension.
i.e. without map you will not get iterator as result. you can substitute lambda with ordinary function, yet you cannot skip map if you want iterator as result
def increase_10(num):
    return  num + 10
    
bar = [1,2,3]
foo = list(map(increase_10, bar))
print(foo)

# and with list comprehension
foo = [n+10 for n in bar]
foo = [increase_10(n) for n in bar]
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020