https://github.com/python/cpython/commit/a85e512699512893f34329b9ddab5ec5ad8a19bb
commit: a85e512699512893f34329b9ddab5ec5ad8a19bb
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: rhettinger <[email protected]>
date: 2024-02-28T17:11:46-06:00
summary:
[3.12] Improve all_equal() recipe (gh-116081) (gh-116083)
files:
M Doc/library/itertools.rst
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 8ae7a304ff12f4..82d24a752a4aaa 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -855,10 +855,9 @@ which incur interpreter overhead.
"Given a predicate that returns True or False, count the True results."
return sum(map(pred, iterable))
- def all_equal(iterable):
+ def all_equal(iterable, key=None):
"Returns True if all the elements are equal to each other."
- g = groupby(iterable)
- return next(g, True) and not next(g, False)
+ return len(take(2, groupby(iterable, key))) <= 1
def first_true(iterable, default=False, pred=None):
"""Returns the first true value in the iterable.
@@ -1217,6 +1216,8 @@ The following recipes have a more mathematical flavor:
>>> [all_equal(s) for s in ('', 'A', 'AAAA', 'AAAB', 'AAABA')]
[True, True, True, False, False]
+ >>> [all_equal(s, key=str.casefold) for s in ('', 'A', 'AaAa', 'AAAB',
'AAABA')]
+ [True, True, True, False, False]
>>> quantify(range(99), lambda x: x%2==0)
50
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]