Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.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.033 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; '3),': 0.09; 'def': 0.10; '1),': 0.16; '2),': 0.16; '2)]': 0.16; 'entry:': 0.16; 'evaluates': 0.16; 'intuition': 0.16; 'lambda': 0.16; 'subject:sub': 0.16; 'true:': 0.16; 'element': 0.17; 'yield': 0.17; '>>>': 0.18; 'elements': 0.23; 'pass': 0.25; 'header:User- Agent:1': 0.26; "doesn't": 0.28; 'e.g.': 0.30; 'function': 0.30; 'code': 0.31; 'problem': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'entry': 0.33; 'list': 0.35; 'something': 0.35; 'there': 0.35; 'charset:us-ascii': 0.36; 'does': 0.37; 'data': 0.37; 'received:10': 0.38; 'to:addr:python.org': 0.39; 'subject:-': 0.40; 'content-disposition:inline': 0.60; 'first': 0.61; 'back': 0.62; 'thomas': 0.62; 'skip:n 10': 0.63; 'sharing': 0.74; 'received:10.94': 0.84; 'solvable': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uni-mainz.de; i=@uni-mainz.de; q=dns/txt; s=ironport; t=1348935353; x=1380471353; h=date:from:to:subject:message-id:mime-version; bh=2K4kF4R8jxbob+fkDTByao5dyVx5LZyl5ijWzCZoBfQ=; b=JKTYtAf+aVwaSXkjTz3Qc+kfllwvgVpGCeAf8hei+F1dDPFcorQh2DBQ mFSDhznQtTKhC31VbvwcIcTbXFCE9lGw9AfFER8GNXZsIf2n08l+XQueH /gA4gf3GS2TnVezR/xH+EHg7MznoVelQGPuXK0ic04atcWmeZbq9xNvqL g=; X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApwEAIwdZ1AKXgZQ/2dsb2JhbABFvziCIAEBBDuBECQpIIgSCph1oQmOSoJAYAOVaAGQK4JpghU Date: Sat, 29 Sep 2012 18:14:38 +0200 From: Thomas Bach To: Subject: Slicing iterables in sub-generators without loosing elements MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-Originating-IP: [94.219.181.175] 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348935354 news.xs4all.nl 6900 [2001:888:2000:d::a6]:35097 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30503 Hi, say we have the following: >>> data = [('foo', 1), ('foo', 2), ('bar', 3), ('bar', 2)] is there a way to code a function iter_in_blocks such that >>> result = [ list(block) for block in iter_in_blocks(data) ] evaluates to >>> result = [ [('foo', 1), ('foo', 2)], [('bar', 3), ('bar', 2)] ] by _only_ _iterating_ over the list (caching all the elements sharing the same first element doesn't count)? I came up with the following def iter_in_blocks(iterable): my_iter = iter(iterable) while True: first = next(my_iter) pred = lambda entry: entry[0] == first[0] def block_iter(): yield first for entry in itertools.takewhile(pred, my_iter): yield entry yield block_iter() which does not work as itertools.takewhile consumes the first entry not fulfilling the pred. I currently have the intuition that the problem is not solvable without using e.g. a global to pass something back to iter_in_blocks from block_iter. Any other suggestions? Regards, Thomas Bach.