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 <> wrote:
> You might be able to avoid calling the method twice using the walrus

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 --
To unsubscribe send an email to
Message archived at
Code of Conduct: