Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'reference:': 0.07; 'python': 0.08; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; 'entries': 0.15; '172': 0.16; 'head:': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'tmp': 0.16; 'wrote:': 0.18; 'memory': 0.21; 'feb': 0.22; 'from:addr:web.de': 0.23; 'index': 0.24; 'import': 0.27; 'yield': 0.29; 'pm,': 0.29; 'chris': 0.30; 'thu,': 0.32; 'header:X -Complaints-To:1': 0.34; 'loop': 0.34; 'steven': 0.34; 'try:': 0.34; 'to:addr:python-list': 0.35; 'list,': 0.36; 'received:org': 0.36; 'else,': 0.37; 'but': 0.37; 'think': 0.38; 'should': 0.38; 'data': 0.38; 'except': 0.39; 'or,': 0.40; 'to:addr:python.org': 0.40; 'huge': 0.61; 'more': 0.61; 'your': 0.61; '1000': 0.62; 'buy': 0.68; 'cycle;': 0.84; 'itertools,': 0.84; '10000': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Cycle around a sequence Date: Thu, 09 Feb 2012 09:33:13 +0100 Organization: None References: <4f3343b2$0$1615$c3e8da3$76491128@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084adf6.dip.t-dialin.net 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1328776409 news.xs4all.nl 6850 [2001:888:2000:d::a6]:36584 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20057 Chris Angelico wrote: > On Thu, Feb 9, 2012 at 2:55 PM, Steven D'Aprano > wrote: >> If your data is humongous but only available lazily, buy more memory :) > > Or if you have a huge iterable and only need a small index into it, > snag those first few entries into a list, then yield everything else, > then yield the saved ones: > def cycle(seq,n): > seq=iter(seq) > lst=[next(seq) for i in range(n)] > try: > while True: yield next(seq) > except StopIteration: > for i in lst: yield i I think that should be spelt def cycle2(seq, n): seq = iter(seq) head = [next(seq) for i in range(n)] for item in seq: yield item for item in head: yield item or, if you are into itertools, def cycle3(seq, n): seq = iter(seq) return chain(seq, list(islice(seq, n))) $ python -m timeit -s'from tmp import cycle; data = range(1000); start=10' 'for item in cycle(data, 10): pass' 1000 loops, best of 3: 358 usec per loop $ python -m timeit -s'from tmp import cycle2; data = range(1000); start=10' 'for item in cycle2(data, 10): pass' 1000 loops, best of 3: 172 usec per loop $ python -m timeit -s'from tmp import cycle3; data = range(1000); start=10' 'for item in cycle3(data, 10): pass' 10000 loops, best of 3: 56.5 usec per loop For reference: $ python -m timeit -s'data = range(1000); start=10' 'for item in data[start:] + data[:start]: pass' 10000 loops, best of 3: 56.4 usec per loop