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