Why is there no builtin to consume a generator? I find that odd.

On Wed, Jun 12, 2013 at 5:28 PM, Serhiy Storchaka <storch...@gmail.com> wrote:
> 12.06.13 09:32, Phil Connell написав(ла):
>>
>> On 12 Jun 2013 01:36, "Roy Smith" <r...@panix.com <mailto:r...@panix.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.
>
>
> any((new_songs if s.is_new() else old_songs).append(s) for s in songs)
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list



-- 
Fábio Santos
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to