I'm not against your proposal at this point, but I think you can
already do the first example:

    clean = [line.strip() for line in lines if line.strip()]

You might be able to avoid calling the method twice using the walrus
operator.  For any operation more complex than that (ie, more complex
than clean_lines, defined below) I'd use the list constructor with a
named function anyway, rather than inlining it in a comprehension.  I
consider that more readable.

Of course, YMMV, which is why I'm not against the proposal at this
point, but to me the point of things like lambda and comprehensions is
precisely that they're one logical line, and frequently
they're one physical line.  So no, the comprehension including a
multiline compound statement is not "easily" nicer for me.

Alex Hall writes:
 > > Yes but then it's the same as defining a generator-function.
 > 
 > List comprehensions are already the same as other things, but
 > they're nice anyway. `lambda` is the same as defining a function,
 > but it's nice too. Syntactic sugar is helpful sometimes. I think
 > this:
 > 
 >     clean = [
 >         for line in lines:
 >             stripped = line.strip()
 >             if stripped:
 >                 yield stripped
 >     ]
 > 
 > is easily nicer than this:
 > 
 >     def clean_lines():
 >         for line in lines:
 >             line = line.strip()
 >             if line:
 >                 yield line
 >     
 >     clean = list(clean_lines())
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/4GQSRURIAY2XELMMUIMQ73N575REIBVA/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to