Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #47788

Re: Split a list into two parts based on a filter?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <fabiosantosart@gmail.com>
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; 'syntax': 0.04; 'true,': 0.05; 'subject:two': 0.07; '[],': 0.09; 'agrees': 0.09; 'iterate': 0.09; 'subject:into': 0.09; 'cc:addr:python-list': 0.11; 'conditional': 0.16; 'exactly,': 0.16; 'expression.': 0.16; 'hack,': 0.16; 'iterator': 0.16; 'lambda': 0.16; 'path:': 0.16; 'smith"': 0.16; 'subject:based': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'memory': 0.22; 'email addr:gmail.com&gt;': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'rid': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '&gt;': 0.26; 'pass': 0.26; 'least': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'point': 0.28; 'wonder': 0.29; 'skip:& 60': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; '(although': 0.31; '>>>>': 0.31; 'concern': 0.31; 'constant': 0.31; 'continuing': 0.33; 'could': 0.34; 'problem': 0.35; 'requirement': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'useful': 0.36; 'subject:?': 0.36; 'list': 0.37; 'skip:o 20': 0.38; 'skip:& 10': 0.38; 'list,': 0.38; 'fact': 0.38; 'anything': 0.39; 'sure': 0.39; 'everybody': 0.60; 'liked': 0.60; 'course': 0.61; "you're": 0.61; 'more': 0.64; 'side': 0.67; 'article': 0.77; 'phil': 0.84; 'seriously,': 0.84; 'usage.': 0.84; '\xa0but,': 0.84; 'effects.': 0.91; 'technique': 0.93; '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 :cc:content-type; bh=ix+CHWJoUQuyF7cbCVtULwzxMWCCHWouBIKe44dqEJg=; b=sBksTSFMvJepjf29kJEMotkeMWBl4EULeXt8/8q+HuxoX1gug2+DmxCS+RkBC8ropJ xP1eE0qQgUkpvRzUH2kyhI7HxeSWRctgyZklyR4at0w6jPQ7KtLq5s3cpIAxZyeB3Lpa cxc/dJFv3717JyK8Qse3262sh2Rp76c605PyWa0deoI3dB/erxydbZvzZXagHTSQrScR t9PV+IATC945eNGbSWppUtIFStQBZz21SGvYDdvB4wcqtOxCqkcpXEDnejKa0w4gAowm wgqOTQ3joiwmSCp1V8HGhzq7lnTvYc7TqtSNTnpSt87gTrwUcE4VLehm/IDZ/sgCUp7R YEwg==
MIME-Version 1.0
X-Received by 10.229.166.193 with SMTP id n1mr7410628qcy.113.1371037914546; Wed, 12 Jun 2013 04:51:54 -0700 (PDT)
In-Reply-To <roy-84E0BB.07395112062013@news.panix.com>
References <kp5d9e$2hf$1@panix2.panix.com> <mailman.2992.1370904643.3114.python-list@python.org> <roy-12F8FA.00111311062013@news.panix.com> <mailman.3023.1370964449.3114.python-list@python.org> <roy-10ED1B.20334411062013@news.panix.com> <mailman.3050.1371018754.3114.python-list@python.org> <roy-84E0BB.07395112062013@news.panix.com>
Date Wed, 12 Jun 2013 12:51:54 +0100
Subject Re: Split a list into two parts based on a filter?
From Fábio Santos <fabiosantosart@gmail.com>
To Roy Smith <roy@panix.com>
Content-Type multipart/alternative; boundary=e89a8f647595dec7be04def3a37c
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3064.1371037923.3114.python-list@python.org> (permalink)
Lines 104
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1371037923 news.xs4all.nl 15993 [2001:888:2000:d::a6]:37435
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:47788

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On 12 Jun 2013 12:43, "Roy Smith" <roy@panix.com> wrote:
>
> In article <mailman.3050.1371018754.3114.python-list@python.org>,
>  Phil Connell <pconnell@gmail.com> wrote:
>
> > > Well, continuing down this somewhat bizarre path:
> > >
> > > new_songs, old_songs = [], []
> > > itertools.takewhile(
> > >     lambda x: True,
> > >     (new_songs if s.is_new() else old_songs).append(s) for s in songs)
> > >     )
> > >
> > > I'm not sure I got the syntax exactly right, but the idea is anything
> > > that will iterate over a generator expression.  That at least gets rid
> > > of the memory requirement to hold the throw-away list :-)
> >
> > You could equivalently pass the generator to deque() with maxlen=0 -
this
> > consumes the iterator with constant memory usage.
> >
> > We are of course firmly in the twilight zone at this point (although
this
> > can be a useful technique in general).
>
> We've been in the twilight zone for a while.  That's when the fun
> starts.  But, somewhat more seriously, I wonder what, exactly, it is
> that freaks people out about:
>
> >>>> [(new_songs if s.is_new() else old_songs).append(s) for s in songs]
>
> Clearly, it's not the fact that it build and immediately discards a
> list, because that concern is addressed with the generator hack, and I
> think everybody (myself included) agrees that's just horrible.

I think someone has already said that the problem is that you're creating
the list comprehension just for the side effects.

> Or, is it the use of the conditional to create the target for append()?

I particularly liked that part.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Split a list into two parts based on a filter? roy@panix.com (Roy Smith) - 2013-06-10 16:34 -0400
  Re: Split a list into two parts based on a filter? Chris Angelico <rosuav@gmail.com> - 2013-06-11 08:50 +1000
  Re: Split a list into two parts based on a filter? Roel Schroeven <roel@roelschroeven.net> - 2013-06-11 00:50 +0200
    Re: Split a list into two parts based on a filter? Roy Smith <roy@panix.com> - 2013-06-11 00:11 -0400
      Re: Split a list into two parts based on a filter? Serhiy Storchaka <storchaka@gmail.com> - 2013-06-11 18:27 +0300
        Re: Split a list into two parts based on a filter? Roy Smith <roy@panix.com> - 2013-06-11 20:33 -0400
          Re: Split a list into two parts based on a filter? Phil Connell <pconnell@gmail.com> - 2013-06-12 07:32 +0100
            Re: Split a list into two parts based on a filter? Roy Smith <roy@panix.com> - 2013-06-12 07:39 -0400
              Re: Split a list into two parts based on a filter? Fábio Santos <fabiosantosart@gmail.com> - 2013-06-12 12:51 +0100
              Re: Split a list into two parts based on a filter? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-06-12 15:06 +0300
              Re: Split a list into two parts based on a filter? Terry Reedy <tjreedy@udel.edu> - 2013-06-12 14:07 -0400
          Re: Split a list into two parts based on a filter? Serhiy Storchaka <storchaka@gmail.com> - 2013-06-12 19:28 +0300
          Re: Split a list into two parts based on a filter? Fábio Santos <fabiosantosart@gmail.com> - 2013-06-12 17:57 +0100
          Re: Split a list into two parts based on a filter? Terry Reedy <tjreedy@udel.edu> - 2013-06-12 14:47 -0400
          Re: Split a list into two parts based on a filter? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-06-13 10:43 +0100
  Re: Split a list into two parts based on a filter? Chris Rebert <clp2@rebertia.com> - 2013-06-10 16:03 -0700
  Re: Split a list into two parts based on a filter? Tim Chase <python.list@tim.thechases.com> - 2013-06-10 18:10 -0500
  Re: Split a list into two parts based on a filter? Fábio Santos <fabiosantosart@gmail.com> - 2013-06-11 00:08 +0100
    Re: Split a list into two parts based on a filter? alex23 <wuwei23@gmail.com> - 2013-06-11 17:44 -0700
  Re: Split a list into two parts based on a filter? Chris Angelico <rosuav@gmail.com> - 2013-06-11 09:12 +1000
  Re: Split a list into two parts based on a filter? Peter Otten <__peter__@web.de> - 2013-06-11 02:11 +0200
  Re: Split a list into two parts based on a filter? Peter Otten <__peter__@web.de> - 2013-06-11 08:43 +0200
  Re: Split a list into two parts based on a filter? Jonas Geiregat <jonas@geiregat.org> - 2013-06-11 08:47 +0200
  Re: Split a list into two parts based on a filter? Fábio Santos <fabiosantosart@gmail.com> - 2013-06-11 14:48 +0100
    Re: Split a list into two parts based on a filter? rusi <rustompmody@gmail.com> - 2013-06-11 09:37 -0700
      Re: Split a list into two parts based on a filter? Fábio Santos <fabiosantosart@gmail.com> - 2013-06-11 18:05 +0100
        Re: Split a list into two parts based on a filter? rusi <rustompmody@gmail.com> - 2013-06-11 10:23 -0700
          Re: Split a list into two parts based on a filter? Chris Angelico <rosuav@gmail.com> - 2013-06-12 03:37 +1000
            Re: Split a list into two parts based on a filter? rusi <rustompmody@gmail.com> - 2013-06-11 11:13 -0700
          Re: Split a list into two parts based on a filter? Fábio Santos <fabiosantosart@gmail.com> - 2013-06-11 19:05 +0100
  Re: Split a list into two parts based on a filter? Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-11 15:22 +0100
  Re: Split a list into two parts based on a filter? Serhiy Storchaka <storchaka@gmail.com> - 2013-06-11 18:28 +0300
  Re: Split a list into two parts based on a filter? Chris Angelico <rosuav@gmail.com> - 2013-06-12 03:28 +1000
    Re: Split a list into two parts based on a filter? Roy Smith <roy@panix.com> - 2013-06-11 20:12 -0400
  Re: Split a list into two parts based on a filter? Peter Otten <__peter__@web.de> - 2013-06-11 20:13 +0200
  Re: Split a list into two parts based on a filter? Peter Otten <__peter__@web.de> - 2013-06-11 20:18 +0200
  Re: Split a list into two parts based on a filter? Chris Angelico <rosuav@gmail.com> - 2013-06-12 04:27 +1000
  Re: Split a list into two parts based on a filter? Roel Schroeven <roel@roelschroeven.net> - 2013-06-11 22:22 +0200

csiph-web