Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'correct.': 0.07; 'none)': 0.09; 'preferable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sequences.': 0.09; 'def': 0.12; 'anyway': 0.14; '24,': 0.16; '3):': 0.16; 'garbage': 0.16; 'incorrect': 0.16; 'iterable': 0.16; 'iterables': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'itertools': 0.16; 'notation': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'succinct': 0.16; 'wrote:': 0.18; 'wed,': 0.18; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'raise': 0.29; 'dec': 0.30; 'start,': 0.30; 'subject:learning': 0.31; 'problem': 0.35; "can't": 0.35; 'but': 0.35; 'version': 0.36; 'really': 0.36; 'opposed': 0.36; 'returning': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'ian': 0.60; 'results.': 0.60; 'course': 0.61; 'skip:n 10': 0.64; 'more': 0.64; 'natural': 0.68; 'hand': 0.80; 'abc': 0.84; 'n):': 0.84; 'silently': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: learning to use iterators Date: Thu, 25 Dec 2014 11:34:38 +0100 Organization: None References: <878uhyb3hq.fsf@net82.ceos.umanitoba.ca> <8a840698-a3be-4889-9ddd-756a553e6f48@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bdbf45.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1419503693 news.xs4all.nl 2875 [2001:888:2000:d::a6]:45732 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82912 Ian Kelly wrote: > On Wed, Dec 24, 2014 at 1:34 PM, Rustom Mody > wrote: >> +1 for the slice in succinct form > > Not only more succinct but also more correct. The purpose of islice is to > slice arbitrary iterables as opposed to just sequences. But this function > requires a reentrant iterable anyway and returns garbage if you pass it an > iterator, so there's really no reason for it here. The version using slice > notation on the other hand will raise a TypeError if you pass it an > iterator or anything else that can't be sliced, which is preferable to > silently returning incorrect results. The natural remedy to that problem is of course itertools.tee(): >>> from itertools import islice, tee >>> def n_grams(items, n): ... z = (islice(it, start, None) for start, it in enumerate(tee(items, n))) ... return zip(*z) ... >>> for item in n_grams(iter("abcde"), 3): ... print("".join(item)) ... abc bcd cde >>>