Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 08d01ee

Browse files
committed
Add partition recipe to itertools docs.
1 parent e9499ae commit 08d01ee

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

Doc/library/itertools.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,12 @@ which incur interpreter overhead.
653653
pending -= 1
654654
nexts = cycle(islice(nexts, pending))
655655
656+
def partition(pred, iterable):
657+
'Use a predicate to partition entries into false entries and true entries'
658+
# partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
659+
t1, t2 = tee(iterable)
660+
return filterfalse(pred, t1), filter(pred, t2)
661+
656662
def powerset(iterable):
657663
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
658664
s = list(iterable)

0 commit comments

Comments
 (0)