slicing the end of a string in a list

Paul Rubin http
Fri Mar 3 00:48:18 EST 2006


John Salerno <johnjsal at NOSPAMgmail.com> writes:
> Interesting. So I would say:
> 
> [line.rstrip() for line in open('C:\\switches.txt')]

Yes, you could do that.  Note that it builds up an in-memory list of
all those lines, instead of processing the file one line at a time.
If the file is very large, that might be a problem.

If you use parentheses instead:

  (line.rstrip() for line in open('C:\\switches.txt'))

you get what's called a generator expression that you can loop
through, but that's a bit complicated to explain, it's probably better
to get used to other parts of Python before worrying about that.



More information about the Python-list mailing list