Skip to content

Instantly share code, notes, and snippets.

@Ceasar
Created February 8, 2013 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ceasar/4739874 to your computer and use it in GitHub Desktop.
Save Ceasar/4739874 to your computer and use it in GitHub Desktop.
Take a predicate and a list and return a pair of lists: those elements that do and do not satisfy the predicate, respectively.
def partition(pred, items):
"""
Take a predicate and a list and return a pair of lists: those elements
that do and do not satisfy the predicate, respectively.
>>> partition(str.isalpha, "ab1c")
(['a', 'b', 'c'], ['1'])
>>> partition(str.isalpha, [])
([], [])
"""
trues, falses = [], []
for item in items:
if pred(item):
trues.append(item)
else:
falses.append(item)
return trues, falses
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment