<p dir="ltr"><br>
On 12 Jun 2013 01:36, "Roy Smith" <<a href="mailto:roy@panix.com">roy@panix.com</a>> wrote:<br>
><br>
> In article <<a href="mailto:mailman.3023.1370964449.3114.python-list@python.org">mailman.3023.1370964449.3114.python-list@python.org</a>>,<br>
> Serhiy Storchaka <<a href="mailto:storchaka@gmail.com">storchaka@gmail.com</a>> wrote:<br>
><br>
> > 11.06.13 07:11, Roy Smith написав(ла):<br>
> > > In article <<a href="mailto:mailman.2992.1370904643.3114.python-list@python.org">mailman.2992.1370904643.3114.python-list@python.org</a>>,<br>
> > > Roel Schroeven <<a href="mailto:roel@roelschroeven.net">roel@roelschroeven.net</a>> wrote:<br>
> > ><br>
> > >> new_songs, old_songs = [], []<br>
> > >> [(new_songs if s.is_new() else old_songs).append(s) for s in songs]<br>
> > ><br>
> > > Thanks kind of neat, thanks.<br>
> > ><br>
> > > I'm trying to figure out what list gets created and discarded. I think<br>
> > > it's [None] * len(songs).<br>
> ><br>
> > It is the same as your klunky code, but consumes more memory.<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 :-)</p>
<p dir="ltr">You could equivalently pass the generator to deque() with maxlen=0 - this consumes the iterator with constant memory usage. </p>
<p dir="ltr">We are of course firmly in the twilight zone at this point (although this can be a useful technique in general). </p>
<p dir="ltr">Cheers, <br>
Phil</p>
<p dir="ltr">><br>
> --<br>
> <a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br>
><br>
</p>