Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'rest,': 0.07; 'none):': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'runtime': 0.09; 'duplicating': 0.16; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'mylist': 0.16; 'paulo': 0.16; 'route.': 0.16; 'sublist': 0.16; 'subject:list': 0.21; 'header:In-Reply-To:1': 0.22; 'though.': 0.23; 'stefan': 0.24; 'code': 0.25; 'writes:': 0.25; 'import': 0.27; 'paul': 0.27; 'constant': 0.28; "skip:' 10": 0.29; 'totally': 0.32; "isn't": 0.32; 'header:User-Agent:1': 0.33; 'instead': 0.33; 'to:addr:python-list': 0.33; 'it?': 0.33; 'loop': 0.34; 'list.': 0.34; '(not': 0.34; 'header:X-Complaints-To:1': 0.34; 'element': 0.36; 'run': 0.37; 'received:org': 0.37; 'think': 0.38; 'received:de': 0.39; 'subject:from': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'your': 0.61; '1000': 0.62; 'storage': 0.68; "'it": 0.84; 'overhead,': 0.84; 'subject:2nd': 0.84; 'subject:huge': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Stefan Behnel Subject: Re: Iterate from 2nd element of a huge list Date: Wed, 01 Feb 2012 12:28:33 +0100 References: <7xy5sm6h3i.fsf@ruckus.brouhaha.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: fw-snc-frn5-de01.fw.telefonica.de User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:9.0) Gecko/20111220 Thunderbird/9.0 In-Reply-To: <7xy5sm6h3i.fsf@ruckus.brouhaha.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328095735 news.xs4all.nl 6878 [2001:888:2000:d::a6]:58159 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19706 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