Howto flaten a list of lists was (Explanation of this Python languagefeature)

M

Mark H Harris

Can anyone - maybe one of the Python language core team, or someone
with knowledge of the internals of Python - can explain why this
code works, and whether the different occurrences of the name x in
the expression, are in different scopes or not? :

x = [[1,2], [3,4], [5,6]]
[x for x in x for x in x]
I'll give this +1 for playfulness, and -2 for lack of clarity.
I hope no one thinks this sort of thing is good to do in real-life code.

No. This has to be a better way to flatten lists:
[1, 2, 3, 4, 5, 6, 7, 8, 9]


marcus
 
M

Mark Lawrence

code works, and whether the different occurrences of the name x in >>
the expression, are in different scopes or not? :
x = [[1,2], [3,4], [5,6]]
[x for x in x for x in x]
I'll give this +1 for playfulness, and -2 for lack of clarity.
I hope no one thinks this sort of thing is good to do in real-life code.

Strange, I thought Dan Stromberg wrote the above.
No. This has to be a better way to flatten lists:
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Why reinvent yet another way of flattening lists, particulary one that
doesn't use the far more sensible:-

from operator import add

As for the stupid symbol that you're using, real programmers don't give
a damn about such things, they prefer writing plain, simple, boring code
that is easy to read.
 
M

Mark H Harris

No. This has to be a better way to flatten lists:
from functools import reduce
import operator as λ
reduce(λ.add, l)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Why reinvent yet another way of flattening lists, particulary one that
doesn't use the far more sensible:-

{ particularly }
from operator import add

As for the stupid symbol that you're using, real programmers don't give
a damn about such things, they prefer writing plain, simple, boring code
that is easy to read.

:)) as RMS would say, "playful hacking, dude, playful hacking..."
 
M

Mark H Harris

No. This has to be a better way to flatten lists:
from functools import reduce
import operator as λ
reduce(λ.add, l)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Why reinvent yet another way of flattening lists, particulary one that
doesn't use the far more sensible:-

{ particularly }
from operator import add

As for the stupid symbol that you're using, real programmers don't give
a damn about such things, they prefer writing plain, simple, boring code
that is easy to read.

:)) as RMS would say, "playful hacking, dude, playful hacking..."
 
S

Steven D'Aprano

Mark, please stop posting to the newsgroup comp.lang.python AND the
mailing list (e-mail address removed). They mirror each other. Your posts
are not so important that we need to see everything twice.
 
M

Mark H Harris

Mark, please stop posting to the newsgroup comp.lang.python AND the
mailing list (e-mail address removed). They mirror each other. Your posts
are not so important that we need to see everything twice.

Its not my fault, Steven. Something goofy is going on. My address says
only comp.lang.python

I have no idea why some of these messages are being duplicated on the
mailing list. I only post to the news group.

Anyways, sorry. I'll keep checking this.

regards,
 
R

Rustom Mody

Its not my fault, Steven. Something goofy is going on. My address says
only comp.lang.python
I have no idea why some of these messages are being duplicated on the
mailing list. I only post to the news group.
Anyways, sorry. I'll keep checking this.

Just use the amazing, fool-safe, fail-proof google-groups.
And enjoy bliss.

[Uh... And now I need to run... Out of sprinting practice...]
 
M

Mark H Harris

Well, something's causing your messages to come out multiple times and
with different subject lines :)

I changed the subject line ( which I did twice because the first
post said it had an error and did not post; which apparently was a lie).

Then I posted again (this time it went without error, but came back
duplicated. In any case ONLY the news group was in the to: field. Very
strange. All of this crap started happening a couple of days back when
Thunderbird updated their client (I should know better than to accept that).

Anyway, things were supposed to be better once I switched from gg. uh, huh.

Well, at least the line wrapping thing is fixed.

marcus
 
M

Mark Lawrence

Its not my fault, Steven. Something goofy is going on. My address says
only comp.lang.python
I have no idea why some of these messages are being duplicated on the
mailing list. I only post to the news group.
Anyways, sorry. I'll keep checking this.

Just use the amazing, fool-safe, fail-proof google-groups.
And enjoy bliss.

[Uh... And now I need to run... Out of sprinting practice...]

Blast, you beat me to it, and I never could sprint :)
 
M

mm0fmf

As for the stupid symbol that you're using, real programmers don't give
a damn about such things, they prefer writing plain, simple, boring code
that is easy to read

What he said.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top