Iterate from 2nd element of a huge list

Tim Delaney timothy.c.delaney at gmail.com
Tue Jan 31 21:16:20 EST 2012


On 1 February 2012 12:39, Paulo da Silva <p_s_d_a_s_i_l_v_a at netcabo.pt>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?
>

If you are sure that mylist contains at least one element:

>>> mylist = [1, 2, 3]
>>> i = iter(mylist)
>>> print next(i)
1
>>> for el in i:
...     print el
...
2
3

Note: for older pythons, you may need i.next() instead of next(i).

If mylist may be empty, you will need some error handling.

Tim Delaney
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120201/be0a731c/attachment-0001.html>


More information about the Python-list mailing list