Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!news2.arglkargh.de!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.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.017 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'else:': 0.03; 'subject:two': 0.07; '[],': 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; 'expecting': 0.16; 'itertools': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'roy': 0.16; 'subject:based': 0.16; 'version.': 0.19; 'seems': 0.21; 'header:User-Agent:1': 0.23; 'passes': 0.24; 'header:X-Complaints-To:1': 0.27; 'header :In-Reply-To:1': 0.27; 'compared': 0.30; 'subject:list': 0.30; 'along': 0.30; "i'm": 0.30; 'lines': 0.31; 'pascal': 0.31; 'this.': 0.32; 'could': 0.34; 'common': 0.35; 'something': 0.35; 'received:84': 0.35; 'but': 0.35; 'subject:?': 0.36; 'two': 0.37; 'list.': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'such': 0.63; 'skip:n 10': 0.64; 'of:': 0.68; 'smith': 0.68; 'groups.': 0.74; 'divide': 0.84; 'want:': 0.84; 'do:': 0.91; 'songs': 0.91; 'songs,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Roel Schroeven Subject: Re: Split a list into two parts based on a filter? Date: Tue, 11 Jun 2013 00:50:30 +0200 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: d54c6d802.access.telenet.be User-Agent: Thunderbird 2.0.0.24 (Windows/20100228) In-Reply-To: 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370904643 news.xs4all.nl 15982 [2001:888:2000:d::a6]:51807 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47612 Roy Smith schreef: > I have a list, songs, which I want to divide into two groups. > Essentially, I want: > > new_songs = [s for s in songs if s.is_new()] > old_songs = [s for s in songs if not s.is_new()] > > but I don't want to make two passes over the list. I could do: > > new_songs = [] > old_songs = [] > for s in songs: > if s.is_new(): > new_songs.append(s) > else: > old_songs.append(s) > > Which works, but is klunky compared to the two-liner above. This > seems like a common enough thing that I was expecting to find > something in itertools which did this. I'm thinking something along > the lines of: > > matches, non_matches = isplit(lambda s: s.is_new, songs) > > Does such a thing exist? You could do something like: new_songs, old_songs = [], [] [(new_songs if s.is_new() else old_songs).append(s) for s in songs] But I'm not sure that that's any better than the long version. -- "People almost invariably arrive at their beliefs not on the basis of proof but on the basis of what they find attractive." -- Pascal Blaise roel@roelschroeven.net