all pairs of items in a list without indexing?

Alex Martelli aleaxit at
Wed Sep 29 19:16:35 CEST 2004

Steven Bethard <steven.bethard at> wrote:
> So slicing a list and iterating over the slice is faster than iterating over
> the indices and indexing the items...  Is this generally true?  Is it
> something I can depend on across implementations?

Unfortunately, nothing can forbid a new implementation, or even a new
release of an existing implementation, acquiring some special new
optimization that makes a technique that used to be slower suddenly
faster.  And if it _was_ possible to forbid optimizations, you wouldn't
really want to, anyway -- a potential optimization of some _other_
technique wouldn't damage the solution you've chosen, anyway, except in
terms of "relative standing" vs the now-optimized alternative.

Since using the slice is simpler, and at least for now it's also faster
(so it won't become _too_ slow anyway), that's what I would suggest.  As
a precaution you might want to use a 'virtual slice' through a function
such as:

def vslice_from(alist, start):
    ''' returns some iterable x such that "for i in x:" is exactly
        equivalent to "for i in alist[start:]:". '''
    return alist[start:]

so that you can experiment with alternatives without having to modify
most of your code, just through alterations to vslice_from.  E.g.

import itertools
def vslice_from(alist, start, islice=itertools.islice):
    return islice(alist, start, len(alist))


def vslice_from(alist, start):
   for i in xrange(start, len(alist)): yield alist[i]

or even customized pyrex or C implementations if your quest for the
ultimate performance needs them...


More information about the Python-list mailing list