I need the following function:
True if all elements in the input list evaluate as equal to each other using the standard equality operator;
Performance: of course, I prefer not to incur any unnecessary overhead.
I feel it would be best to:
- iterate through the list
- compare adjacent elements
AND all the resulting Boolean values
But I'm not sure what's the most Pythonic way to do that.
Thank you for all the great answers. I rated up several, and it was really hard to choose between @KennyTM and @Ivo van der Wijk solutions.
The lack of short-circuit feature only hurts on a long input (over ~50 elements) that have unequal elements early on. If this occurs often enough (how often depends on how long the lists might be), the short-circuit is required. The best short-circuit algorithm seems to be @KennyTM
checkEqual1. It pays, however, a significant cost for this:
- up to 20x in performance nearly-identical lists
- up to 2.5x in performance on short lists
If the long inputs with early unequal elements don't happen (or happen sufficiently rarely), short-circuit isn't required. Then, by far the fastest is @Ivo van der Wijk solution.