summaryrefslogtreecommitdiff
path: root/Doc/library/itertools.rst
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2016-03-06 18:11:38 -0800
committerRaymond Hettinger <python@rcn.com>2016-03-06 18:11:38 -0800
commit35c3fb54e5487c5cbab04d91e05dca9f374c4a2a (patch)
tree8c692a755ab43a7b7d4b11d2d7093821e7201765 /Doc/library/itertools.rst
parentcd4bee22d84757095a803cbd722316f0b946edfa (diff)
downloadcpython-35c3fb54e5487c5cbab04d91e05dca9f374c4a2a.tar.gz
Document another recipe for itertools: all_equal(). Inspired by David Beazley.
Diffstat (limited to 'Doc/library/itertools.rst')
-rw-r--r--Doc/library/itertools.rst5
1 files changed, 5 insertions, 0 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 9dac5e1921..a9d10736d1 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -693,6 +693,11 @@ which incur interpreter overhead.
"Returns the nth item or a default value"
return next(islice(iterable, n, None), default)
+ def all_equal(iterable):
+ "Returns True if all the elements are equal to each other"
+ g = groupby(iterable)
+ return next(g, True) and not next(g, False)
+
def quantify(iterable, pred=bool):
"Count how many times the predicate is true"
return sum(map(pred, iterable))