Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!news.linkpendium.com!news.linkpendium.com!panix!panix2.panix.com!not-for-mail From: roy@panix.com (Roy Smith) Newsgroups: comp.lang.python Subject: Split a list into two parts based on a filter? Date: 10 Jun 2013 16:34:54 -0400 Organization: PANIX -- Public Access Networks Corp. Lines: 24 Message-ID: NNTP-Posting-Host: panix2.panix.com X-Trace: reader1.panix.com 1370896494 27698 166.84.1.2 (10 Jun 2013 20:34:54 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Mon, 10 Jun 2013 20:34:54 +0000 (UTC) Xref: csiph.com comp.lang.python:47603 I have a list, songs, which I want to divide into two groups. Essentially, I want: new_songs = [s for s in songs if s.is_new()] old_songs = [s for s in songs if not s.is_new()] but I don't want to make two passes over the list. I could do: new_songs = [] old_songs = [] for s in songs: if s.is_new(): new_songs.append(s) else: old_songs.append(s) Which works, but is klunky compared to the two-liner above. This seems like a common enough thing that I was expecting to find something in itertools which did this. I'm thinking something along the lines of: matches, non_matches = isplit(lambda s: s.is_new, songs) Does such a thing exist?