Re:WTFPython: Exploring and understanding Python through sur...
by compressedgas on Tuesday May 31, 2022 @02:30AM
(#31566668)
You can write it this way:
[ Parent
| on HN ]
Re:WTFPython: Exploring and understanding Python through sur...
by jreese on Tuesday May 31, 2022 @03:21AM
(#31566928)
FWIW, that version ends up being slower, because you're constructing and iterating over a tuple for every iteration, which incurs a similar cost to running `.strip()` twice. The sub-generator example I gave is better because you're only constructing the generator expression once, and requires less overhead for each iteration.
Re:WTFPython: Exploring and understanding Python through sur...
by d0mine on Tuesday May 31, 2022 @12:39PM
(#31571651)
It is unlikely (unless there is a benchmark that says otherwise). Before the walrus operator, the single item loop could have been used:
nonblank = [value for line in file for value in [line.strip()] if value]