3 ms·
The parent poster was pointing out that this requires having two in-memory complete copies of the file: [... for row in open(filename).readlines()] The re
by me-vs-cat 3y ago
The parent poster was pointing out that this requires having two in-memory complete copies of the file:
[... for row in open(filename).readlines()]
The readlines return value is one copy, and the list comprehension is another copy. However, that first copy can be avoided with:
[... for row in open(filename)]
The entire file must still be read to evaluate the list comprehension.
Additionally, this doesn't do what you think it does:
>>> header, *records = (row.strip().split(',') for row in f)
Compare to this, using a variable for clarity:
>>> gen = (row.strip().split(',') for row in f)
>>> header, *records = next(gen)