Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!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.034 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'url:pypi': 0.03; 'else:': 0.03; 'subject:two': 0.07; 'similar,': 0.09; 'subject:into': 0.09; 'cc:addr:python-list': 0.11; 'expecting': 0.16; 'itertools': 0.16; 'roy': 0.16; 'sorting': 0.16; 'subject:based': 0.16; 'wrote:': 0.18; 'fit': 0.20; 'seems': 0.21; 'cc:addr:python.org': 0.22; 'module,': 0.24; 'passes': 0.24; 'requirement.': 0.24; 'mon,': 0.24; 'cheers,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; "doesn't": 0.30; 'compared': 0.30; 'raymond': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'along': 0.30; "i'm": 0.30; 'lines': 0.31; 'this.': 0.32; 'url:python': 0.33; 'maybe': 0.34; 'could': 0.34; 'common': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'subject:?': 0.36; 'url:org': 0.36; 'two': 0.37; 'list.': 0.37; 'list,': 0.38; 'pm,': 0.38; 'does': 0.39; 'bill': 0.39; 'enough': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'such': 0.63; 'skip:n 10': 0.64; 'due': 0.66; 'of:': 0.68; 'smith': 0.68; 'groups.': 0.74; 'divide': 0.84; 'sender:addr:chris': 0.84; 'want:': 0.84; 'do:': 0.91; 'songs': 0.91; 'songs,': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=dDlmqxHnG8aA7/dZIQox+an8yqgoJXpNpZjRi90ayro=; b=Jx4c3hLjFYtU3BBG8Dv5uryyLmQB6EKy7LaJjMXFN8jUEaZNCOj0K/SBw43KKIba8P reimkh7UxijHX6xKTpgXnBc2SfGEx8+jm/HfeFehXzywIih7div4JPPqjvxasIe83bQz 8zR9zK1d5jjRGbOjZsmWRLs5OUk/ciJw+JXrs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :x-gm-message-state; bh=dDlmqxHnG8aA7/dZIQox+an8yqgoJXpNpZjRi90ayro=; b=olbXqlVGF8jCoBHv3qeFBNnefJ/5zrmk8EgVnJVtJF3OliCts/FkGEEfZYKS0BiEx9 lVNUgP7row6VPLHZllqRNo1n//KIxNjWSqhmNe5PZjnPiH/em4pKvm+fwdnQOnXF/nl2 bSPiUOpVTwZC8xNNj38q+vSyuXFiEPAIh44Qm/IAOgnHBeyM+RKBY+wf8QAqkV+J99ok cOLpEja2qiiGXza3fTwTwUbwmqeGuxS6XCdlyyScZypOMbfrReIufl7WAXEaS1tBbxmt 9CmwucnSGKGFYRY9GvuNKXuBrhn7o60Ir/BHO4dKPsxiXyzn+hmdkvWbJT0P+LLnOXg3 EwdQ== MIME-Version: 1.0 X-Received: by 10.50.118.97 with SMTP id kl1mr4804503igb.46.1370905422931; Mon, 10 Jun 2013 16:03:42 -0700 (PDT) Sender: chris@rebertia.com In-Reply-To: References: Date: Mon, 10 Jun 2013 16:03:42 -0700 X-Google-Sender-Auth: tVMz-hLbamBfCLfzYt3s7wa47yA Subject: Re: Split a list into two parts based on a filter? From: Chris Rebert To: Roy Smith Content-Type: text/plain; charset=UTF-8 X-Gm-Message-State: ALoCoQnqtfm9LTmdb4LHK4PCt51J1AZQX1JOf/cEF0Zp8rvTAkU1Y1f4l3rmLM17hVedRGOjQF22 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370905425 news.xs4all.nl 15911 [2001:888:2000:d::a6]:59955 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47614 On Mon, Jun 10, 2013 at 1:34 PM, Roy Smith wrote: > 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? itertools.groupby() is kinda similar, but unfortunately doesn't fit the bill due to its sorting requirement. There is regrettably no itertools.partition(). And given how dead-set Raymond seems to be against adding things to the itertools module, there will likely never be. Maybe more-itertools (https://pypi.python.org/pypi/more-itertools ) would accept a patch? Cheers, Chris