Check if all elements in a list are equal
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Checking if all elements in a list are equal is a common operation in programming and data processing. When dealing with lists or arrays, situations often arise where you need to verify that every element is identical. This kind of validation might be critical in ensuring data consistency, detecting anomalies, or applying specific algorithms that require uniform input.
In this article, we will explore various methods to determine whether all elements in a list are equal, focusing on concepts and techniques from Python. We'll delve into multiple approaches, analyze their performance implications, and present examples to clarify their usage.
Methods to Check for Equality in a List
Method 1: Using a Loop
The most straightforward way to check if all elements in a list are equal is by iterating through the list and comparing each element to the first element.
- If the list is empty, it's considered to have all equal elements by default.
- The function iterates over the list, comparing each element to the first one. If a mismatch is found, the function returns `False`.
- If no mismatches are found throughout the iteration, the function returns `True`.
- Convert the list to a set.
- If the set has one or zero elements, all elements in the list are equal. If not, some elements differ.
- The function checks for an empty list and returns `True` if so.
- A generator expression iterates over the list, and `all()` evaluates whether all comparisons are `True`.
- The `count()` method counts occurrences of the first element.
- If the count matches the length of the list, it confirms uniformity.
- Often considered to hold 'all equal elements' since there are no differences to be found.
- If the task is to find lists full of duplicate elements, any of these methods work, but consider switching to `set` or the loop approach for optimal balance between readability and performance.
- Ensure list elements are of comparable types to reliably use equality checks.
Related reading
- Check if an array contains any element of another array in JavaScript
- Check if any item in a list matches any item in another list
- Check if array B is a permutation of A
- Check if edge is included in SOME MST in linear time non-distinct values
- Check if key exists and iterate the JSON array using Python
- Check if list contains element that contains a string and get that element
- Check if item is in an array / list
- Check if one list contains element from the other

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.