Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.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.025 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'anyway.': 0.05; 'subject:two': 0.07; 'subject:into': 0.09; 'def': 0.12; 'deque': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterable': 0.16; 'iterator': 0.16; 'subject:based': 0.16; 'sure.': 0.16; 'modification': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'long,': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'tim': 0.29; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'chase': 0.31; 'lists': 0.32; 'maybe': 0.34; "i'd": 0.34; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'really': 0.36; 'false': 0.36; 'subject:?': 0.36; 'two': 0.37; 'received:209': 0.37; 'performance': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'major': 0.40; 'here:': 0.62; 'making': 0.63; 'optimized': 0.68; 'hand': 0.80; 'end.': 0.84; 'heh.': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=OVXzfoVB4yeusLC7L+M9hVSSO81CBow4AzpYE1thclk=; b=tG9Bjm34BBQ2GJmQOOtxCJumasEmWMzetmIFakiOxfIpT+nWFJjxdVVv5ZYeEYPPj1 D8grekpxqWwMH1d52+2I9ETAbVusUZyqJ7m5dni2z33GiqjtuRPWjnEtrK9GRLlnvOWZ lWzXgpz0SbmkmpL4g3llR1H/pbbHj/PdqT4YN14gs/E89V2WhRV47oRwC44sKOzBtVzj f216KewN4krWxhmeP0mMH/IxHjHIZ/wZUQvDf/enU85yYmL/Pe3/B5ZNPVDB5S2N2/kO JsdcpARCkNRHOKLW2q/BiUTcQ5xiPb0knkpXr7QAFR908pYnDzmPwGCbnRLjKu61zn7v 0tVg== MIME-Version: 1.0 X-Received: by 10.52.164.163 with SMTP id yr3mr5901637vdb.76.1370905978653; Mon, 10 Jun 2013 16:12:58 -0700 (PDT) In-Reply-To: <20130610181022.42c05db6@bigbox.christie.dr> References: <20130610181022.42c05db6@bigbox.christie.dr> Date: Tue, 11 Jun 2013 09:12:58 +1000 Subject: Re: Split a list into two parts based on a filter? From: Chris Angelico To: python-list@python.org 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: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370905986 news.xs4all.nl 15902 [2001:888:2000:d::a6]:38546 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47617 On Tue, Jun 11, 2013 at 9:10 AM, Tim Chase wrote: > On 2013-06-11 08:50, Chris Angelico wrote: >> The iterator version strikes my fancy. Maybe this isn't of use to >> you, but I'm going to try my hand at making one anyway. >> >> >>> def iterpartition(pred,it): >> """Partition an iterable based on a predicate. >> >> Returns two iterables, for those with pred False and those >> True.""" falses,trues=[],[] > > This is really nice. I think the only major modification I'd make is > to use a collections.deque() instead of lists here: > > trues, falses = collections.deque(), collections.deque() > > as I seem to recall that list.pop(0) has some performance issues if > it gets long, while the deque is optimized for fast (O(1)?) push/pop > on either end. Sure. If it's that good I might submit it to more-itertools... heh. ChrisA