Iterate from 2nd element of a huge list

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Jan 31 21:43:38 EST 2012


On Wed, 01 Feb 2012 01:39:38 +0000, Paulo da Silva wrote:

> Hi!
> 
> What is the best way to iterate thru a huge list having the 1st element
> a different process? I.e.:
> 
> process1(mylist[0])
> for el in mylist[1:]:
> 	process2(el)
> 
> This way mylist is almost duplicated, isn't it?

Yes. But don't be too concerned: what you consider a huge list and what 
Python considers a huge list are unlikely to be the same. In my 
experience, many people consider 10,000 items to be huge, but that's only 
about 45K of memory. Copying it will be fast. On my laptop:

steve at runes:~$ python -m timeit -s "L = range(10000)" "L2 = L[1:]"
10000 loops, best of 3: 57.1 usec per loop

But if you have tens of millions of items, or think that you might 
someday have to deal with tens of millions of items, here's an easy 
technique to use:

it = iter(mylist)
process1(next(it))  # In Python 2.5, use it.next() instead.
for el in it:
    process2(el)


No copying is performed.

For tiny lists, the iterator overhead will mean this is a smidgen slower, 
but for tiny lists, who cares if it takes 2 nanoseconds instead of 1?


-- 
Steven



More information about the Python-list mailing list