[edit: This answer addresses the currently top-voted itertools.groupby
(which is a good answer) answer later on.]
Without rewriting the program, the most asymptotically performant and most readable way is as follows:
all(x==myList[0] for x in myList)
(Yes, this even works with the empty list! This is because this is one of the few cases where python has lazy semantics.)
This will fail at the earliest possible time, so it is asymptotically optimal (expected time is approximately O(#uniques) rather than O(N), but worst-case time still O(N)). This is assuming you have not seen the data before...
(If you care about performance but not that much about performance, you can just do the usual standard optimizations first, like hoisting the myList[0]
constant out of the loop and adding clunky logic for the edge case, though this is something the python compiler might eventually learn how to do and thus one should not do it unless absolutely necessary, as it destroys readability for minimal gain.)
If you care slightly more about performance, this is twice as fast as above but a bit more verbose:
def allEqual(iterable):
iterator = iter(iterable)
try:
firstItem = next(iterator)
except StopIteration:
return True
for x in iterator:
if x!=firstItem:
return False
return True
If you care even more about performance (but not enough to rewrite your program), use the currently top-voted itertools.groupby
answer, which is twice as fast as allEqual
because it is probably optimized C code. (According to the docs, it should (similar to this answer) not have any memory overhead because the lazy generator is never evaluated into a list... which one might be worried about, but the pseudocode shows that the grouped 'lists' are actually lazy generators.)
If you care even more about performance read on...
sidenotes regarding performance, because the other answers are talking about it for some unknown reason:
... if you have seen the data before and are likely using a collection data structure of some sort, and you really care about performance, you can get .isAllEqual()
for free O(1) by augmenting your structure with a Counter
that is updated with every insert/delete/etc. operation and just checking if it's of the form {something:someCount}
i.e. len(counter.keys())==1
; alternatively you can keep a Counter on the side in a separate variable. This is provably better than anything else up to constant factor. Perhaps you can also use python's FFI with ctypes
with your chosen method, and perhaps with a heuristic (like if it's a sequence with getitem, then checking first element, last element, then elements in-order).
Of course, there's something to be said for readability.
a == b
or identical as ina is b
? – kennytmfunctools.reduce(operator.eq, a)
hasn't been suggested. – user2846495functools.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) – Nephanth