Path: csiph.com!usenet.pasdenom.info!news.albasani.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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'operator': 0.03; '3),': 0.09; 'sep': 0.09; 'def': 0.10; 'sat,': 0.15; '1),': 0.16; '2),': 0.16; '2)]': 0.16; 'blocks': 0.16; 'evaluates': 0.16; 'itemgetter': 0.16; 'itertools': 0.16; 'subject:sub': 0.16; 'wrote:': 0.17; 'element': 0.17; 'import': 0.21; 'elements': 0.23; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'first.': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'correct': 0.28; '>>>>': 0.29; 'received:209.85.215.46': 0.30; 'function': 0.30; 'code': 0.31; 'could': 0.32; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'so,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'subject:-': 0.40; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'share': 0.61; 'first': 0.61; 'thomas': 0.62; 'sharing': 0.74; 'guaranteed': 0.76; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=eNSWrawOtVw6hioSOPhOWUKUY3p1kV18fh0RMILfOR8=; b=ewawkX7iVKl20IvvKRVJMEQS0byEuqLVU9PFJSJViZD5HhlNDOMI3FeMo4N85cEwTL 1PLts4j4dmDTNSy+Ftv8tPLFUij9jz6FLWed2yKQvSGvmlQ5yW76ngl0wdl+DleiLaeD 1gEvsE3i8gLVLGXRjEaFzw1Ay0zVWy08zwXJzqJ2O1Hv9NU5sOuaHTfLUfUVCdyYSaNa qAkFZevIOwETT08A4QkjgCJYL0JCljzokY160RC702i77zZNECMdTdIbNnotzSfLil4Q co36EANxZAy1Q5I0yG6ucKSgvA6ozgbrTPsAcoKuOfIMzNp+FrYSNiJYbpd2xaa9B83g +eEg== MIME-Version: 1.0 In-Reply-To: <20120929161437.GA8832@taris.box> References: <20120929161437.GA8832@taris.box> From: Ian Kelly Date: Sat, 29 Sep 2012 10:36:39 -0600 Subject: Re: Slicing iterables in sub-generators without loosing elements 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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348936632 news.xs4all.nl 6907 [2001:888:2000:d::a6]:43495 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30506 On Sat, Sep 29, 2012 at 10:14 AM, Thomas Bach wrote: > 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)? Am I correct in understanding that the intent is that the "blocks" are groups that share the same first item? Is it guaranteed that the blocks will be contiguous? If so, you could use itertools.groupby: from itertools import groupby, imap from operator import itemgetter def iter_in_blocks(data): return imap(itemgetter(1), groupby(data, itemgetter(0))) If there is no such guarantee, then the list would need to be sorted first.