Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19706

Re: Iterate from 2nd element of a huge list

From Stefan Behnel <stefan_ml@behnel.de>
Subject Re: Iterate from 2nd element of a huge list
Date 2012-02-01 12:28 +0100
References <jga54q$vqg$1@speranza.aioe.org> <7xy5sm6h3i.fsf@ruckus.brouhaha.com>
Newsgroups comp.lang.python
Message-ID <mailman.5299.1328095735.27778.python-list@python.org> (permalink)

Show all headers | View raw


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

Stefan

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Iterate from 2nd element of a huge list Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2012-02-01 01:39 +0000
  Re: Iterate from 2nd element of a huge list Cameron Simpson <cs@zip.com.au> - 2012-02-01 13:02 +1100
  Re: Iterate from 2nd element of a huge list duncan smith <buzzard@urubu.freeserve.co.uk> - 2012-02-01 02:31 +0000
  Re: Iterate from 2nd element of a huge list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-01 02:43 +0000
  Re: Iterate from 2nd element of a huge list Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2012-02-01 03:16 +0000
    Re: Iterate from 2nd element of a huge list Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2012-02-01 03:34 +0000
      Re: Iterate from 2nd element of a huge list Cameron Simpson <cs@zip.com.au> - 2012-02-01 15:55 +1100
        Re: Iterate from 2nd element of a huge list Paulo da Silva <p_s_d_a_s_i_l_v_a@netcabo.pt> - 2012-02-02 07:23 +0000
          Re: Iterate from 2nd element of a huge list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-02 07:38 +0000
    Re: Iterate from 2nd element of a huge list Arnaud Delobelle <arnodel@gmail.com> - 2012-02-01 07:09 +0000
    Re: Iterate from 2nd element of a huge list Peter Otten <__peter__@web.de> - 2012-02-01 09:11 +0100
    Re: Iterate from 2nd element of a huge list Arnaud Delobelle <arnodel@gmail.com> - 2012-02-01 10:54 +0000
  Re: Iterate from 2nd element of a huge list Paul Rubin <no.email@nospam.invalid> - 2012-02-01 01:25 -0800
    Re: Iterate from 2nd element of a huge list Stefan Behnel <stefan_ml@behnel.de> - 2012-02-01 12:28 +0100

csiph-web