You have to consider that for the reader of the code that's one line containing a function call versus six lines containing that comprehension. An advantage of functions is that you can hide the implementation but a comprehension always contains all the code. If the function name is clear about what it does you don't have to look at the function itself. So I prefer to read

lines = list(clean())

versus a six lines long list comprehension (though the function name could be improved).
On 2/22/20, 07:57 Alex Hall <alex.mojaki@gmail.com> wrote:
> You might be able to avoid calling the method twice using the walrus
operator.

I specifically discussed the walrus operator solution, but both you and Dominik Vilsmeier seem to have missed that.

> I'd use the list constructor with a
> named function anyway, rather than inlining it in a comprehension. I
> consider that more readable.

I'm curious, how do you find this:

def clean():
for line in lines:
line = line.strip()
if line:
yield line

clean_lines = list(clean())

more readable than this?

clean_lines = [
for line in lines:
line = line.strip()
if line:
yield line
]

It's not that I find my version particularly readable, but I don't see how it's worse.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@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/UNMZTO7QGYD53SUWSFMGZEVUPEIOSAVF/
Code of Conduct: http://python.org/psf/codeofconduct/