Created
February 8, 2013 15:52
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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