Fw: [Python-Dev] Use for enumerate()

Raymond Hettinger python@rcn.com
Sat, 27 Apr 2002 13:20:59 -0400


> > > > Challenge 3: do it faster and with less code.
> > it should work even if the file is too large
> > to fit in memory (as long as each individual line fits in memory).

def getline(filename, lineno):
    if lineno < 1:
        return ''
    f = open(filename)
    g = f.readline
    return (reduce(lambda x,y: g(), range(lineno), ''), f.close())[0]


This incorporates a couple of ideas inspired by Tim's post.


Raymond Hettinger