[Python-Dev] Use for enumerate()

Raymond Hettinger python@rcn.com
Sat, 27 Apr 2002 12:52:08 -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):    # Second attempt
    if lineno < 1:
        return ''
    f = open(filename)
    reduce(lambda x,y: f.readline(), xrange(lineno-1), None)
    return (f.readline(), f.close())[0]

Arghh, must resist lambda.
Must not use reduce.
Must avoid tuple tricks.
GvR's code is too powerful
I can't resist

Raymond Hettinger