Skip to content

Instantly share code, notes, and snippets.

@mdellavo
Created January 22, 2016 16:35
Show Gist options
  • Save mdellavo/46455bd4567c3e981169 to your computer and use it in GitHub Desktop.
Save mdellavo/46455bd4567c3e981169 to your computer and use it in GitHub Desktop.
def divide(collection, decider):
"""
Divide the items of collection into A and B according to decider(item)
:param collection: a sequence of items
:param decider: a function that takes an item and returns true of false
:return: a pair of lists (A, B)
>>> divide(range(10), lambda x: x%2 == 0)
([0, 2, 4, 6, 8], [1, 3, 5, 7, 9])
"""
a = []
b = []
for item in collection:
target = a if decider(item) else b
target.append(item)
return a, b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment