read lines

Peter Otten __peter__ at web.de
Tue Dec 4 10:11:58 EST 2007


Bruno Desthuilliers wrote:

> # You don't need to read the whole file in memory

> lines1, lines2 = tee(infile)
> print min(extract_numbers(lines1)), max(extract_numbers(lines2))

tee() internally maintains a list of items that were seen by
one but not all of the iterators returned. Therefore after calling min()
and before calling max() you have a list of one float per line in memory
which is quite close conceptually to reading the whole file in memory.

If you want to use memory efficiently, stick with the for-loop.

Peter



More information about the Python-list mailing list