Iterate from 2nd element of a huge list

Paul Rubin no.email at nospam.invalid
Wed Feb 1 04:25:05 EST 2012


Paulo da Silva <p_s_d_a_s_i_l_v_a at netcabo.pt> 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.



More information about the Python-list mailing list