Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'anyway.': 0.05; 'subject:two': 0.07; 'subject:into': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '-tkc': 0.16; 'deque': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'iterable': 0.16; 'iterator': 0.16; 'subject:based': 0.16; 'modification': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'long,': 0.26; 'gets': 0.27; 'header:In- Reply-To:1': 0.27; 'chris': 0.29; 'subject:list': 0.30; "i'm": 0.30; 'lists': 0.32; 'maybe': 0.34; "i'd": 0.34; 'but': 0.35; 'version': 0.36; 'really': 0.36; 'false': 0.36; 'charset:us- ascii': 0.36; 'subject:?': 0.36; 'two': 0.37; 'performance': 0.37; 'either': 0.39; 'major': 0.40; 'here:': 0.62; 'making': 0.63; 'to:addr:gmail.com': 0.65; 'optimized': 0.68; 'hand': 0.80; 'end.': 0.84; 'received:50.22': 0.84 Date: Mon, 10 Jun 2013 18:10:22 -0500 From: Tim Chase To: Chris Angelico Subject: Re: Split a list into two parts based on a filter? In-Reply-To: References: X-Mailer: Claws Mail 3.7.6 (GTK+ 2.20.1; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com Cc: python-list@python.org 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: 23 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370905719 news.xs4all.nl 15951 [2001:888:2000:d::a6]:35001 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47615 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. -tkc