Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

Chris Angelico rosuav at gmail.com
Tue Mar 25 01:43:40 EDT 2014


On Tue, Mar 25, 2014 at 4:28 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Tue, 25 Mar 2014 06:57:19 +1100, Chris Angelico wrote:
>
>> Can you imagine a high level language without a simple notation for
>> variable assignment? Certainly not.
>
> I don't know... what a stack based or concatenative language like Forth,
> Postscript, Joy or Factor count as high-level?
>
> I'm pretty sure Joy and Factor should. Perhaps not Forth or Postscript.
>
> Some of these language may offer variable assignment, but using it is
> entirely optional. It's a convenience, nothing more.

Ah, sorry. I meant in languages that do have a concept of variable
assignment. Yes, some don't have it at all, so of course they don't
offer any notation for it. But any reasonably modern symbolic language
is not going to have a long and wordy notation for assignment, because
assignment is so common. It's language design Huffman coding: the
features programmers are expected to use frequently are given
convenient notations. A stack-based language will have extremely
convenient notations for "push this onto the stack", something like
"Put potatoes into the mixing bowl. Put dijon mustard into the mixing
bowl. Put lard into the mixing bowl." (okay, so Chef might not be the
best language to demonstrate this with!).

A basic iterative for loop is considered extremely common. C has a
highly compact notation that packs three parts into a simple header;
REXX has a more piece-meal approach whereby "DO I=start" can be
followed by any combination of "TO stop", "BY step", "WHILE
condition", "UNTIL condition", and "FOR count", each of which has the
same effect on its own or in combination with others. (I think WHILE
and UNTIL might be incompatible with each other, but none of the
others conflict. Having TO and FOR in the same loop header means it'll
stop when either condition is reached.) And Python doesn't have any
such construct, but *only* a 'foreach'. That's what I was talking
about; and yet it works quite nicely.

ChrisA



More information about the Python-list mailing list