473,402 Members | 2,072 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,402 software developers and data experts.

List comp bug?

I seem to have stumbled across a problem with list comprehensions (or
perhaps it's a misunderstanding on my part)

[f() for f in [lambda: t for t in ((1, 2), (3, 4))]]

is giving me

[(3, 4), (3, 4)]

The equivalent using a generator expression:

[f() for f in (lambda: t for t in ((1, 2), (3, 4)))]

is giving me

[(1, 2), (3, 4)]

as expected.

Is this a known bug? I'm using Python 2.4.3

Thanks

James

Apr 13 '06 #1
3 1083
ta*******@gmail.com writes:
I seem to have stumbled across a problem with list comprehensions (or
perhaps it's a misunderstanding on my part)

[f() for f in [lambda: t for t in ((1, 2), (3, 4))]]


List comprehensions are syntactic sugar for "for" loops. The
thing to notice is that in "lambda: t", t is unbound. It's not
the same as the loop index inside the list comp. Try:

[f() for f in [lambda t=t: t for t in ((1, 2), (3, 4))]]
Apr 13 '06 #2
ta*******@gmail.com wrote:
I seem to have stumbled across a problem with list comprehensions (or
perhaps it's a misunderstanding on my part)

[f() for f in [lambda: t for t in ((1, 2), (3, 4))]]

is giving me

[(3, 4), (3, 4)]

The equivalent using a generator expression:

[f() for f in (lambda: t for t in ((1, 2), (3, 4)))]

is giving me

[(1, 2), (3, 4)]

as expected.

Is this a known bug?


no, it's not a bug, it's a consequence of the order in which things are done,
and different visibility rules for the loop variables used in list comprehensions
and generator expressions.

both the inner loops generates lambda expressions that return the value of
the "t" variable.

for the list comprehension, "t" is an ordinary variable (part of the surrounding
scope), and since the comprehension is completed before the resulting list is
returned, all lambdas end up referring to the same "t", which is set to the last
value:
x = [lambda: t for t in ((1, 2), (3, 4))]
x [<function <lambda> at 0x00A87AB0>, <function <lambda> at 0x00A876F0>] x[0]() (3, 4) t = "hello"
x[0]()

'hello'

in contrast, the generator expression uses a local loop variable "t", and only fetches
one value from the target sequence for each iteration, so all lambdas will be bound
to different objects.

any special reason you need to write this kind of convoluted code, btw?

</F>

Apr 13 '06 #3

<ta*******@gmail.com> wrote in message
news:11*********************@v46g2000cwv.googlegro ups.com...
[f() for f in [lambda: t for t in ((1, 2), (3, 4))]]
is giving me
[(3, 4), (3, 4)]

The equivalent using a generator expression:
List comps were originally designed to be equivalent to for loops with
nested for loops and if blocks. Generator expressions are know to be
slightly different and *not* exactly equivalent due to the new local
namespace.
[f() for f in (lambda: t for t in ((1, 2), (3, 4)))]
is giving me
[(1, 2), (3, 4)]


Since, when there is a difference, as above, the genex behavior is more
often the right or expected behavior (as above), Guido is considering
making the diference go away in 3.0 by making [stuff] == list((stuff)) (ie,
listcomp == list(genex)).

Terry Jan Reedy

Apr 13 '06 #4

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

Similar topics

0
by: Boris Ammerlaan | last post by:
This notice is posted about every week. I'll endeavor to use the same subject line so that those of you who have seen it can kill-file the subject; additionally, Supersedes: headers are used to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.