This closely ties to the discussion with subject that starts with “is this PEP-able? [...]” What about [x until condition for x in l ...] or [x for x in l until condition] Condition checked with output values only On Jun 28, 2013, at 3:28 PM, Jan Kaliszewski <zuo@chopin.edu.pl> wrote:
27.06.2013 13:49, Andrew Barnert napisał: [...]
If someone wants this:
x = [value for value in iterable if value > 0 while value < 10]
… and rejects this:
x = [value for value in takewhile(lambda x: x < 10, iterable) if value > 0]
… I don't think they'd be happier with this:
@in x = [value for value in takewhile(under10, iterable) if value > 0] def under10(value): return value < 10 [...] In fact, I think what people really want is the opposite: to write an expression, not a function. That's a big part of the appeal of using comprehensions over map and filter: if you don't already have a ready-made function, no problem (and no lambdas or partials), just use a comprehension and write the expression in-place. People want a similar answer to make takewhile at least as unnecessary as map and filter.
+1!
Maybe it could be achieved with a new separate generator expression syntax, being orthogonal to the existing generator-expr/comprehension syntaxes?
Maybe such as:
(<name> from <iterable> while <condition>)
E.g.:
g = (line from myfile while line.strip())
...which would be equivalent to:
def _temp(_myfile): iterator = iter(_myfile) line = next(iterator) while line.strip(): yield line line = next(iterator) g = _temp(myfile)
Unlike the existing generator-expr/comprehension syntaxes this syntax is focused on predicate-based iteration stopping (like itertools.takewhile) rather than on any processing of the iterated values.
Obviously, it could be combined with the existing syntaxes, e.g.:
processed = [2 * x for x in (x from seq while x < 100)]
...or probably better (a matter of taste):
items = (x from seq while x < 100) processed = [2 * x for x in items]
Cheers. *j
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas