Split a list into two parts based on a filter?
Terry Reedy
tjreedy at udel.edu
Wed Jun 12 14:47:24 EDT 2013
On 6/12/2013 12:57 PM, Fábio Santos wrote:
> Why is there no builtin to consume a generator? I find that odd.
There are several builtins than consume generators -- and do something
useful with the yielded objects. What you mean is "Why is there no
builtin to uselessly consume a generator?" The question almost answers
itself. A generator generates objects to be used.
> On Wed, Jun 12, 2013 at 5:28 PM, Serhiy Storchaka <storchaka at gmail.com> wrote:
>> 12.06.13 09:32, Phil Connell написав(ла):
>>> 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)
The problem here is that the generator generates and yields an unwanted
sequence of None (references) from the append operations. The proper
loop statement
for s in songs:
(new_songs if s.is_new() else old_songs).append(s)
simply ignores the None return of the appends. Since it does not yield
None over and over, no extra code is needed to ignore what should be
ignored in the first place.
--
Terry Jan Reedy
More information about the Python-list
mailing list