map del efficiency python2.2 and 2.1

Alex Martelli aleax at aleax.it
Tue Jul 16 11:53:43 EDT 2002


John Hunter wrote:
        ...
> def pairs(l):
>     return [(l[i], l[i+1]) for i in range(0, len(l), 2)]

In 2.2, with "from __future__ import generators" at the start
of your module, you can probably use this almost as fast as
the other, less-elegant solution, by recoding it as a generator:

def pairs(L):
    for i in xrange(0, len(L), 2):
        yield L[i], L[i+1]

but that's a minor issue, I guess.

Another likely performance boost in 2.2 would be:

m = dict(pairs(seq))

with pairs coded as here shown.

Regarding del time, you could measure that directly by
doing a "del m" and seeing times before and after; also,
just in case (shouldn't matter, but...), you could try
gc.disable() and see if that makes a difference.


Alex




More information about the Python-list mailing list