]> granicus.if.org Git - python/commitdiff
Add partition recipe to itertools docs.
authorRaymond Hettinger <python@rcn.com>
Sat, 7 Aug 2010 05:36:53 +0000 (05:36 +0000)
committerRaymond Hettinger <python@rcn.com>
Sat, 7 Aug 2010 05:36:53 +0000 (05:36 +0000)
Doc/library/itertools.rst

index 8037bfe996c5d016876d09b7652816edcac69378..cadd0f3a6a1bacaf4c6532bb2d2b74b6439b4cec 100644 (file)
@@ -653,6 +653,12 @@ which incur interpreter overhead.
                pending -= 1
                nexts = cycle(islice(nexts, pending))
 
+   def partition(pred, iterable):
+       'Use a predicate to partition entries into false entries and true entries'
+       # partition(is_odd, range(10)) --> 0 2 4 6 8   and  1 3 5 7 9
+       t1, t2 = tee(iterable)
+       return filterfalse(pred, t1), filter(pred, t2)
+
    def powerset(iterable):
        "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
        s = list(iterable)