Iterate from 2nd element of a huge list

Stefan Behnel stefan_ml at
Wed Feb 1 12:28:33 CET 2012

Paul Rubin, 01.02.2012 10:25:
> Paulo da Silva writes:
>> process1(mylist[0])
>> for el in mylist[1:]:
>> 	process2(el)
>> This way mylist is almost duplicated, isn't it?
> I think it's cleanest to use itertools.islice to get the big sublist
> (not tested):
>    from itertools import islice
>    process1 (mylist[0])
>    for el in islice(mylist, 1, None):
>        process2 (el)
> The islice has a small, constant amount of storage overhead instead of
> duplicating almost the whole list.

It also has a tiny runtime overhead, though. So, if your code is totally
performance critical and you really just want to strip off the first
element and then run through all the rest, it may still be better to go the
iter() + next() route.

python3.3 -m timeit -s 'l=list(range(100000))' \
                       'it = iter(l); next(it); all(it)'
1000 loops, best of 3: 935 usec per loop

python3.3 -m timeit -s 'l=list(range(100000))' \
                    -s 'from itertools import islice' \
                    'all(islice(l, 1, None))'
1000 loops, best of 3: 1.63 msec per loop


More information about the Python-list mailing list