Split a list into two parts based on a filter?

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Jun 13 05:43:13 EDT 2013


On 12 June 2013 19:47, Terry Reedy <tjreedy at udel.edu> wrote:
> The proper loop statement
>
> for s in songs:
>     (new_songs if s.is_new() else old_songs).append(s)

I think I would just end up rewriting this as

for s in songs:
    if s.is_new():
        new_songs.append(s)
    else:
        old_songs.append(s)

but then we're back where we started. I don't think any of the
solutions posted in this thread have been better than this. If you
want to make this a nice one-liner then just put this code in a
function.


Oscar



More information about the Python-list mailing list