Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'anyway.': 0.05; 'subject:two': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'false,': 0.09; 'items)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:into': 0.09; 'val': 0.09; 'python': 0.11; 'def': 0.12; 'hmm.': 0.16; 'item)': 0.16; 'item:': 0.16; 'iterable': 0.16; 'iterator': 0.16; 'itertools': 0.16; 'lambda': 0.16; 'operation,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'roy': 0.16; 'subject:based': 0.16; 'true:': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'header:X-Complaints- To:1': 0.27; 'idea': 0.28; 'chris': 0.29; 'am,': 0.29; 'statement': 0.30; 'subject:list': 0.30; "i'm": 0.30; 'maybe': 0.34; 'could': 0.34; 'basic': 0.35; 'something': 0.35; 'but': 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; 'skip:p 20': 0.39; 'received:org': 0.40; '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 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Split a list into two parts based on a filter? Date: Tue, 11 Jun 2013 02:11:04 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p508489f5.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 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: 73 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370909462 news.xs4all.nl 15865 [2001:888:2000:d::a6]:57587 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47621 Chris Angelico wrote: > 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() An alternative implementation, based on itertools.tee: import itertools def partition(items, predicate=bool): a, b = itertools.tee((predicate(item), item) for item in items) return ((item for pred, item in a if not pred), (item for pred, item in b if pred)) if __name__ == "__main__": false, true = partition(range(10), lambda item: item % 2) print(list(false)) print(list(true)) def echo_odd(item): print("checking", item) return item % 2 false, true = partition(range(10), echo_odd) print("FALSE", [next(false) for _ in range(3)]) print("TRUE", next(true)) print("FALSE", list(false)) print("TRUE", list(true))