On Mon, Oct 17, 2016 at 11:16:03AM -0700, Brendan Barnwell wrote: > Now, personally, I don't insist on that invariant. I would > certainly like to be able to do more general things in a list > comprehension,
I hear you, because I too would like to introduce a variant comprehension that uses a while instead of if. So don't think I'm not sympathetic. But that's not an option, and given the official position on comprehensions, I don't think this should be either. Officially, list comprehensions are not a replacement for general for-loops. Python is not Perl, where we encourage people to write one-liners, nor is it Haskell, where everything is an expression. If you want to do "more general things", use a real for-loop. Comprehensions are targetted at a narrow but important and common set of use-cases. > and many times I have been irritated by the fact that the > one-item-per-loop invariant exists. I'm not sure whether I'm in favor of > this particular syntax, but I'd like to be able to do the kind of things it > allows. But doing them inherently requires breaking the invariant you > describe. That last point is incorrect. You already can do the kind of things this thread is about: [*t for t in iterable] # proposed syntax: flatten can be written as: [x for t in iterable for x in t] If you want to apply a function to each element of t: [func(x) for x in [*t for t in iterable]] # proposed syntax can be written today: [func(x) for t in iterable for x in t] If you want to apply a function or expression to t first: [*func(t) for t in iterable] # proposed syntax [*expression for t in iterable] # proposed syntax this too can be written today: [x for t in iterable for x in func(t)] [x for t in iterable for x in expression] You might have an opinion on whether it is better to have an explicit extra loop (more verbose, but less magical) or special syntax (more compact, but more magical), but I don't think this proposal adds anything that cannot be done today. -- Steve _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/