473,395 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Mix lambda and list comprehension?

Hi,
trying to mix lambda expresions and list comprehension
doesn't seem to work.
---
[lambda x:x+y for y in range(10)][9](2) 11 [lambda x:x+y for y in range(10)][4](2)

11
---
I expected the second expression to return 6.
What did I do wrong? Any hints?
Thanks
- Peter
Jul 18 '05 #1
4 3016
Try this:
[lambda x, y=y:x+y for y in range(10)][4](2)
6
It is important to bind y in a closure at the time
the lambda is defined. Otherwise, y remains unbound
until you invoke the function call. At that time, the most
recent value of y is the last value in the range loop (namely, 9).
Raymond Hettinger

"Peter Barth" <pe*********@t-online.de> wrote in message
news:6f**************************@posting.google.c om...
Hi,
trying to mix lambda expresions and list comprehension
doesn't seem to work.
--- [lambda x:x+y for y in range(10)][9](2) 11 [lambda x:x+y for y in range(10)][4](2)

11
---
I expected the second expression to return 6.
What did I do wrong? Any hints?
Thanks
- Peter

Jul 18 '05 #2
pe*********@t-online.de (Peter Barth) wrote in message news:<6f**************************@posting.google. com>...
Hi,
trying to mix lambda expresions and list comprehension
doesn't seem to work.
---
[lambda x:x+y for y in range(10)][9](2) 11 [lambda x:x+y for y in range(10)][4](2) 11
---
I expected the second expression to return 6.
What did I do wrong? Any hints?
Thanks
- Peter


It is a scope issue. The last value for y is used for all
the created lambdas. All lambdas users are bitten by that,
soon or later. The solution is to make y local to the
lambda function, with the optional argument trick:
[lambda x,y=y:x+y for y in range(10)][4](2)

6

Michele
Jul 18 '05 #3
Thanks a lot, works fine.
However, the solution does not really feel "pythonesque".
Is it considered a usability bug or fine as is?
- Peter

"Raymond Hettinger" <vz******@verizon.net> wrote in message news:<hM***************@nwrdny01.gnilink.net>...
Try this:
[lambda x, y=y:x+y for y in range(10)][4](2) 6
It is important to bind y in a closure at the time
the lambda is defined. Otherwise, y remains unbound
until you invoke the function call. At that time, the most
recent value of y is the last value in the range loop (namely, 9).
Raymond Hettinger

"Peter Barth" <pe*********@t-online.de> wrote in message
news:6f**************************@posting.google.c om...
Hi,
trying to mix lambda expresions and list comprehension
doesn't seem to work.
---
>> [lambda x:x+y for y in range(10)][9](2)

11>> [lambda x:x+y for y in range(10)][4](2)

11
---
I expected the second expression to return 6.
What did I do wrong? Any hints?
Thanks
- Peter

Jul 18 '05 #4
bo**@oz.net (Bengt Richter) wrote in message news:<bf**********@216.39.172.122>...
On 15 Jul 2003 05:41:02 -0700, mi**@pitt.edu (Michele Simionato) wrote:
pe*********@t-online.de (Peter Barth) wrote in message news:<6f**************************@posting.google. com>...
Hi,
trying to mix lambda expresions and list comprehension
doesn't seem to work.
---
>>> [lambda x:x+y for y in range(10)][9](2) 11 >>> [lambda x:x+y for y in range(10)][4](2)
11
---
I expected the second expression to return 6.
What did I do wrong? Any hints?
Thanks
- Peter


It is a scope issue. The last value for y is used for all
the created lambdas. All lambdas users are bitten by that,
soon or later. The solution is to make y local to the
lambda function, with the optional argument trick:
> [lambda x,y=y:x+y for y in range(10)][4](2)

6


or you could capture y as constants in the lambdas ;-)
>>> [eval('lambda x:x+%s'%y) for y in range(10)][4](2)

6
Regards,
Bengt Richter


Thanks to God, there is a smile in your post!!

;)

Michele
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chas Emerick | last post by:
In a comment off a post on lambda-the-ultimate, I noticed this little offhand remark: '''IIRC GvR is going to kill the various lambda, map,filter & reduce leaving just generator expressions/list...
53
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions...
26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
30
by: Mike Meyer | last post by:
I know, lambda bashing (and defending) in the group is one of the most popular ways to avoid writing code. However, while staring at some Oz code, I noticed a feature that would seem to make both...
6
by: jena | last post by:
hello, when i create list of lambdas: l=] then l() returns 'C', i think, it should be 'A' my workaround is to define helper class with __call__ method: class X: def __init__(self,s): self.s=s...
22
by: Cameron Laird | last post by:
QOTW: "... and to my utter surprise it worked." - Andrew Nagel on his move from wxPython to programming Tkinter in desperation "Python has more web application frameworks than keywords." - Skip...
2
by: sofeng | last post by:
I would like to use the following recipe to transpose a list of lists with different lengths. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410687 Here is an example of what I would...
21
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.