Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'discard': 0.05; 'chunk': 0.07; 'python': 0.09; '[0,': 0.09; 'before.': 0.09; 'lst': 0.09; 'consume': 0.16; 'iterable': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'iterator.': 0.16; 'iterators': 0.16; 'itertools': 0.16; 'recipe': 0.16; 'subject:doc': 0.16; 'wrote:': 0.17; 'skip': 0.17; 'yield': 0.17; 'examples': 0.18; 'trying': 0.21; 'object.': 0.22; 'example': 0.23; 'this:': 0.23; "i've": 0.23; 'seems': 0.23; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'implicitly': 0.29; 'fri,': 0.30; 'function': 0.30; 'figure': 0.30; 'url:python': 0.32; 'print': 0.32; 'to:addr:python-list': 0.33; 'equal': 0.33; 'front': 0.33; 'version': 0.34; 'received:google.com': 0.34; 'list': 0.35; 'whatever': 0.35; 'pm,': 0.35; 'something': 0.35; 'list.': 0.35; 'really': 0.36; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'anything': 0.36; 'should': 0.36; 'does': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'things': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'list,': 0.39; 'your': 0.60; 'range': 0.60; 'here:': 0.62; '26,': 0.65; 'special': 0.73; '2013': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=/lMCh2VzjvBpvKk38M2xawxN6S76Rhw5D70Hxe/Bfh0=; b=uo8PjLImYokuxk8PYCID3ZGmBtd0Cpr0zEUSeZdeY3JTL/KhGe1kn3tbo1OsaFPAFK KqbV2Lfs1UudTwRnPkJ/TNu/eYpUpaf/Th2vHZJKkejDdO5jNTcDCW1xSPagkW7zEzda 0PC+Ar68FSI6vE1NTteuZURxOoZWVRY4eUYadC0+HP7dBtGHsLNKYX+uIU9NKMM9/ug0 dXGCoSJE2z994O3KJYATNj+uH9xjHLtKMUVj7I03vPLd7nL5heNEIhpGBiMfvgW0099u GRIGx1WzvhP1mbpSw4SC717f5hL+MNLyVE8ItJ6zM4TvLVKWKZoqzLQzvNqWsB7RZQMo UNPQ== X-Received: by 10.220.156.8 with SMTP id u8mr1506549vcw.24.1362775313214; Fri, 08 Mar 2013 12:41:53 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Fri, 8 Mar 2013 13:41:12 -0700 Subject: Re: itertools doc example "consume" To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362775321 news.xs4all.nl 6887 [2001:888:2000:d::a6]:39321 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40899 On Fri, Mar 8, 2013 at 1:28 PM, Skip Montanaro wrote: > I've never really used itertools before. While trying to figure out > how to break a list up into equal pieces, I came across the consume > function in the examples here: > > http://docs.python.org/2/library/itertools.html > > It seems to me that it should return whatever it consumes from the > list. I thought you would call it like this: > > for chunk in consume(range(30), 5): > print chunk > > and see it print something like > > [0, 1, 2, 3, 4] > [5, 6, 7, 8, 9] > ... > [25, 26, 27, 28, 29] That's what the 'grouper' recipe does. The 'consume' recipe is just for removing unwanted things from the front of an iterator. > If I call it like this: > > lst = range(30) > consume(lst, 5) > > it doesn't actually consume anything from lst. Am I missing > something, or is that example missing a return or yield statement? Depending on your Python version lst is either a range object or a list, neither of which is an iterator. If you pass to consume an iterable object that is not an iterator, it will implicitly obtain an iterator for it, consume from the iterator, and then discard the iterator, with no effect on the original object. In general the itertools functions will work equally well on iterators and other iterables, but consume is special in that what it does is only relevant to iterators.