summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2016-03-07 02:11:38 (GMT)
committerRaymond Hettinger <python@rcn.com>2016-03-07 02:11:38 (GMT)
commite525ee3b483889313b62b09b4c9103d18dd9cbb7 (patch)
tree8530e435acbe1737a06eb7fe0439181215e593bb /Doc
parentd66dd5ce68cbf4a33c385439d5eeb2bff4e860f1 (diff)
downloadcpython-e525ee3b483889313b62b09b4c9103d18dd9cbb7.zip
cpython-e525ee3b483889313b62b09b4c9103d18dd9cbb7.tar.gz
cpython-e525ee3b483889313b62b09b4c9103d18dd9cbb7.tar.bz2
Document another recipe for itertools: all_equal(). Inspired by David Beazley.
Diffstat (limited to 'Doc')
-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 9dac5e1..a9d1073 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))