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!newsfeed4.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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'anyway.': 0.05; 'subject:two': 0.07; 'subject:into': 0.09; 'useless': 0.09; 'val': 0.09; 'python': 0.11; 'def': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hmm.': 0.16; 'iterable': 0.16; 'iterator': 0.16; 'operation,': 0.16; 'roy': 0.16; 'subject:based': 0.16; 'true:': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'am,': 0.29; 'statement': 0.30; 'subject:list': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'maybe': 0.34; 'could': 0.34; 'basic': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'false': 0.36; 'yield': 0.36; 'subject:?': 0.36; 'two': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'remove': 0.60; 'break': 0.61; 'making': 0.63; 'more': 0.64; 'smith': 0.68; 'useful.': 0.68; 'hand': 0.80; 'song.': 0.84; 'songs': 0.91; '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=zOjBFpB4QmBvCA0XhkskKymt6tvQ1PnnKVjUIhuvtdE=; b=nUdEL2R9SIghitfLwcYUNN7Y7G45ceNxEzirSsTjlbrPBXUpgRJLWbB9nFLcFWkpnr w5DTqWYR0mQT/s/X1GYww4+p+1dD6u2TVvPl3a8K7n2sVyo2UPd6u3nQ/T7yc0Y3JxDk sFa+A4EygS8Pvwf22Vhv1pAqud7B89eoLaDRTbJzqiaDx4DszATvj5U226mMuqElVwhD mV4RmvSW8UOaly5SU/fxFQ40EQRz87W/nsO4sBierMQZPvFSy5jY1tYaHAOcPY8lol1y 0MLBLZVKFXvWZ/bBL/h7rjv4glCMP1hsqiwF5u/uWN7EwwBXF+APK7zotnsmO3G8ecdA FMsw== MIME-Version: 1.0 X-Received: by 10.220.109.66 with SMTP id i2mr6916960vcp.51.1370904629248; Mon, 10 Jun 2013 15:50:29 -0700 (PDT) In-Reply-To: References: Date: Tue, 11 Jun 2013 08:50:29 +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: 69 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370904631 news.xs4all.nl 15977 [2001:888:2000:d::a6]:51671 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47611 On Tue, Jun 11, 2013 at 6:34 AM, Roy Smith wrote: > new_songs = [s for s in songs if s.is_new()] > old_songs = [s for s in songs if not s.is_new()] Hmm. Would this serve? old_songs = songs[:] new_songs = [songs.remove(s) or s for s in songs if s.is_new()] Python doesn't, AFAIK, have a "destructive remove and return" operation, and del is a statement rather than an expression/operator, but maybe this basic idea could be refined into something more useful. It guarantees to call is_new only once per song. 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=[],[] it=iter(it) def get_false(): while True: if falses: yield falses.pop(0) else: while True: val=next(it) if pred(val): trues.append(val) else: break yield val def get_true(): while True: if trues: yield trues.pop(0) else: while True: val=next(it) if not pred(val): falses.append(val) else: break yield val return get_false(),get_true() >>> f,t=iterpartition(lambda x: x%3,range(100000000)) >>> next(t) 1 >>> next(t) 2 >>> next(t) 4 >>> next(t) 5 >>> next(f) 0 >>> next(f) 3 >>> next(f) 6 >>> next(f) 9 >>> next(f) 12 >>> next(t) 7 >>> next(t) 8 Ha. :) Useless but fun. ChrisA