Python Forum
List comprehension and Lambda
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List comprehension and Lambda
#1
Let's think easy case. Use the map function like this:

test = map(lambda x: x, [1, 2, 3])
This gives us an iteratable object. To process this:

[i for i in test]
gives us a boring result, or a list
[1, 2, 3]
.
The equivalent way by using list comprehension is simply this one.

[lambda x: x for x in [1, 2, 3]]
The list comprehension seems to compress the map function with lambda into a simple list form.

However, here is the problem.
Let's say if we want to get something closure-d, the literal way to use the map function is:

test = map(lambda x: lambda : x, [1, 2, 3])
and, we can get its result like this:

>>> [i() for i in test]
[1, 2, 3]
>>> 
This is like a lazy evaluation. Each element of the test is like a promise, which SICP tells us.
However, the may-be-equivalent list comprehension version gives us a weird result.

>>> test = [lambda : x for x in [1, 2, 3]]
>>> [i() for i in test]
[3, 3, 3]
>>> 


Wow. What we expect to get is a list [1, 2, 3], but it gives back a list [3, 3, 3].

Is this a sort of bug, or there a detailed specification?
I'm stuck with it.

Smile Thanks, regards.
Reply
#2
test = [lambda : x for x in [1, 2, 3]]
I think the list comprehension assigns all values for x, overwriting the object that x is pointing to each time and leaves x pointing to the last object.
[i() for i in test]
when the lambda function is called it gives the object that x is pointing to which is the last object.

If it's done as a generator it will give the object that x is pointing to on each iteration.
test = (lambda : x for x in [1, 2, 3])
print([i() for i in test])
Output:
[1, 2, 3]
Reply
#3
(Jun-08-2021, 08:09 AM)Yoriz Wrote: I think the list comprehension generates all the assigned values for x, overwriting x each time and leaves x pointing to the last value.
Oh, I see. I've never imagined that Python's list comprehension is implemented with a destructive, or non-functional way, even though it comes from Haskell.
(Would Haskell give the same result? I don't know now. I'll test whether.)

O.K. I'll try generator instead.

Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 440 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 1,176 Apr-02-2023, 06:31 PM
Last Post: tester_V
  list comprehension 3lnyn0 4 1,360 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  List comprehension used differently coder_sw99 3 1,680 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,753 Jul-17-2021, 04:30 PM
Last Post: maiya
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,176 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  For Loop with List Comprehension muzikman 25 6,411 Dec-18-2020, 10:45 PM
Last Post: muzikman
  Sorting list of names using a lambda (PyBite #5) Drone4four 2 2,683 Oct-16-2020, 07:30 PM
Last Post: ndc85430
  Using recursion instead of for loops / list comprehension Drone4four 4 3,073 Oct-10-2020, 05:53 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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