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

Ian Kelly ian.g.kelly at gmail.com
Mon Mar 24 20:42:03 CET 2014


On Mon, Mar 24, 2014 at 1:12 PM, Chris Angelico <rosuav at gmail.com> wrote:
> Incidentally, I've often modified my loop counter, in C or REXX or any
> other language. About the only situation where I actually miss it in
> Python, though, is iterating over a list and mutating the list on the
> way through; and even that can often be done in other ways (maybe a
> list comp, filtering out some of the elements?). It's amazing how
> something can be so utterly fundamental (I mean, come ON! Who can
> imagine a language with no equivalent of the basic "do i=1 to 10"
> (REXX) or "for (int i=0;i<10;++i)" (C++) loop???) and yet so
> dispensable.

I'm not sure "fundamental" is the right word.  A for loop is just a
while loop with some syntactic sugar.  For that matter, a while loop
is just a structured goto...



More information about the Python-list mailing list