Split a list into two parts based on a filter?

Roel Schroeven roel at roelschroeven.net
Tue Jun 11 16:22:53 EDT 2013


Peter Otten schreef:
> Fábio Santos wrote:
> 
>> On 10 Jun 2013 23:54, "Roel Schroeven" <roel at roelschroeven.net> wrote:
>>> You could do something like:
>>>
>>> new_songs, old_songs = [], []
>>> [(new_songs if s.is_new() else old_songs).append(s) for s in songs]
>>>
>>> But I'm not sure that that's any better than the long version.
>> This is so beautiful!
> 
> It makes me cringe. 
> 
> This code does spurious work to turn what is naturally written as a for loop 
> into a list comprehension that is thrown away immediately. 

I agree. I see two problems with it: it's not very readable or pythonic, 
and it creates a list that is not needed and is immediately thrown away.

I wrote that code just to see if I could make it work that way. In real 
code I would never write it that way, let's make that clear :)


-- 
"People almost invariably arrive at their beliefs not on the basis of
proof but on the basis of what they find attractive."
         -- Pascal Blaise

roel at roelschroeven.net




More information about the Python-list mailing list