#Python istas: there is

with open(file, 'r') as f:
for line in f:
line = line.strip() # <this>

dostuff(line) # example

Is there a way to inline <this> somehow somewhere in the code part before dostuff(), so that I iterate over lines in f and have a pre-stripped line variable at hand right away to do stuff with?

Follow

@cherti Would this work?

for line in (raw.strip() for raw in f):
...

Sign in to participate in the conversation
CleverLibre Social

CleverLibre Social is an inclusive social instance for open discussion, learning, and community.
All cultures welcome.
Hate speech and harassment strictly forbidden.