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: 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>': 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; '>': 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: References: Date: Wed, 12 Jun 2013 12:51:54 +0100 Subject: Re: Split a list into two parts based on a filter? From: =?ISO-8859-1?Q?F=E1bio_Santos?= To: Roy Smith 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --e89a8f647595dec7be04def3a37c Content-Type: text/plain; charset=ISO-8859-1 On 12 Jun 2013 12:43, "Roy Smith" wrote: > > In article , > Phil Connell 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. --e89a8f647595dec7be04def3a37c Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable


On 12 Jun 2013 12:43, "Roy Smith" <roy@panix.com> wrote:
>
> In article <mailman.3050.1371018754.3114.python-list@python.org>= ,
> =A0Phil Connell <pconnell@gma= il.com> wrote:
>
> > > Well, continuing down this somewhat bizarre path:
> > >
> > > new_songs, old_songs =3D [], []
> > > itertools.takewhile(
> > > =A0 =A0 lambda x: True,
> > > =A0 =A0 (new_songs if s.is_new() else old_songs).append(s) f= or s in songs)
> > > =A0 =A0 )
> > >
> > > I'm not sure I got the syntax exactly right, but the ide= a is anything
> > > that will iterate over a generator expression. =A0That at le= ast gets rid
> > > of the memory requirement to hold the throw-away list :-) > >
> > You could equivalently pass the generator to deque() with maxlen= =3D0 - this
> > consumes the iterator with constant memory usage.
> >
> > We are of course firmly in the twilight zone at this point (altho= ugh this
> > can be a useful technique in general).
>
> We've been in the twilight zone for a while. =A0That's when th= e fun
> starts. =A0But, 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) f= or 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 yo= u're creating the list comprehension just for the side effects.

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

I particularly liked that part.

--e89a8f647595dec7be04def3a37c--