More versions using itertools.groupby
that I find clearer than the original (more about that below):
def all_equal(iterable):
g = groupby(iterable)
return not any(g) or not any(g)
def all_equal(iterable):
g = groupby(iterable)
next(g, None)
return not next(g, False)
def all_equal(iterable):
g = groupby(iterable)
return not next(g, False) or not next(g, False)
Here's the original from the Itertools Recipes again:
def all_equal(iterable):
g = groupby(iterable)
return next(g, True) and not next(g, False)
Note that the next(g, True)
is always true (it's either a non-empty tuple
or True
). That means its value doesn't matter. It's executed purely for advancing the groupby
iterator. But including it in the return
expression leads the reader into thinking that its value gets used there. Since it doesn't, I find that misleading and unnecessarily complicated. My second version above treats the next(g, True)
as what it's actually used for, as a statement whose value we don't care about.
My third version goes a different direction and does use the value of the first next(g, False)
. If there isn't even a first group at all (i.e., if the given iterable is "empty"), then that solution returns the result right away and doesn't even check whether there's a second group.
My first solution is basically the same as my third, just using any
. Both solutions read as "All elements are equal iff ... there is no first group or there is no second group."
Benchmark results (although speed is really not my point here, clarity is, and in practice if there are many equal values, most of the time might be spent by the groupby
itself, reducing the impact of these differences here):
Python 3.10.4 on my Windows laptop:
iterable = ()
914 ns 914 ns 916 ns use_first_any
917 ns 925 ns 925 ns use_first_next
1074 ns 1075 ns 1075 ns next_as_statement
1081 ns 1083 ns 1084 ns original
iterable = (1,)
1290 ns 1290 ns 1291 ns next_as_statement
1303 ns 1307 ns 1307 ns use_first_next
1306 ns 1307 ns 1309 ns use_first_any
1318 ns 1319 ns 1320 ns original
iterable = (1, 2)
1463 ns 1464 ns 1467 ns use_first_any
1463 ns 1463 ns 1467 ns next_as_statement
1477 ns 1479 ns 1481 ns use_first_next
1487 ns 1489 ns 1492 ns original
Python 3.10.4 on a Debian Google Compute Engine instance:
iterable = ()
234 ns 234 ns 234 ns use_first_any
234 ns 235 ns 235 ns use_first_next
264 ns 264 ns 264 ns next_as_statement
265 ns 265 ns 265 ns original
iterable = (1,)
308 ns 308 ns 308 ns next_as_statement
315 ns 315 ns 315 ns original
316 ns 316 ns 317 ns use_first_any
317 ns 317 ns 317 ns use_first_next
iterable = (1, 2)
361 ns 361 ns 361 ns next_as_statement
367 ns 367 ns 367 ns original
384 ns 385 ns 385 ns use_first_next
386 ns 387 ns 387 ns use_first_any
Benchmark code:
from timeit import timeit
from random import shuffle
from bisect import insort
from itertools import groupby
def original(iterable):
g = groupby(iterable)
return next(g, True) and not next(g, False)
def use_first_any(iterable):
g = groupby(iterable)
return not any(g) or not any(g)
def next_as_statement(iterable):
g = groupby(iterable)
next(g, None)
return not next(g, False)
def use_first_next(iterable):
g = groupby(iterable)
return not next(g, False) or not next(g, False)
funcs = [original, use_first_any, next_as_statement, use_first_next]
for iterable in (), (1,), (1, 2):
print(f'{iterable = }')
times = {func: [] for func in funcs}
for _ in range(1000):
shuffle(funcs)
for func in funcs:
number = 1000
t = timeit(lambda: func(iterable), number=number) / number
insort(times[func], t)
for func in sorted(funcs, key=times.get):
print(*('%4d ns ' % round(t * 1e9) for t in times[func][:3]), func.__name__)
print()
a == b
or identical as ina is b
?functools.reduce(operator.eq, a)
hasn't been suggested.functools.reduce(operator.eq, a)
would not work. for example with the list[True, False, False]
, it would return((True == False) == False)
which isTrue
. Whereas the function should return False (because the elements are not all equal)