Efficiently iterating over part of a list

Peter Otten __peter__ at web.de
Fri Oct 13 04:05:39 EDT 2006


Steven D'Aprano wrote:

> Are there better or more Pythonic alternatives to this obvious C-like
> idiom?
> 
> for i in range(1, len(alist)):
>     x = alist[i]

For small start values you can use itertools.islice(), e. g:

for x in islice(alist, 1, None):
    # use x

You'd have to time at what point the C-like idiom (which I would have no
qualms using throughout) becomes faster.

Peter




More information about the Python-list mailing list