With Python, we can check if all elements in a list are equal by converting the list to a set and checking if the set has length 1.
def checkAllElementsEqual(lst):
return len(set(lst)) == 1
print(checkAllElementsEqual([0,1,2,3,4]))
print(checkAllElementsEqual([0,0,0,0,0]))
#Output:
False
True
When working with collections of data in a Python program, it’s possible you want to check if all elements in an array are equal.
Arrays in Python are called lists, and we can easily check if all elements in a list are equal.
To check if all items in a list are equal, the easily way is to convert the list to a set and check the set’s length. If the length of the set is 1, then we know that all elements are equal.
Below is a simple function in Python of how to check if all elements of a list are the same.
def checkAllElementsEqual(lst):
return len(set(lst)) == 1
print(checkAllElementsEqual([0,1,2,3,4]))
print(checkAllElementsEqual([0,0,0,0,0]))
#Output:
False
True
There are a few other ways you can check if all elements in a list are equal in Python which you can read about below.
Using a Loop to Check if All Elements in a List are the Same in Python
Another method we can use to check if all elements in a list are the same is to use a for loop.
To check if all items in a list are equal with a loop, we just check if all elements are equal to the first element.
Below is a Python function which will check if all elements of a list are equal with a for loop.
def checkAllElementsEqual(lst):
for x in lst:
if lst[0] != x:
return False
return True
print(checkAllElementsEqual([0,1,2,3,4]))
print(checkAllElementsEqual([0,0,0,0,0]))
#Output:
False
True
Using count() to Check if All Items in a List are the Same in Python
Another way we can check if all items in a list are equal is with the help of the count() function.
The count() function in Python gives us the count of how many times a particular value is found in a list.
If a list has all equal values, then the count of the first value should equal the length of the list.
Below is how to check if all elements in a list are equal with the help of the count() function.
def checkAllElementsEqual(lst):
return lst.count(lst[0]) == len(lst)
print(checkAllElementsEqual([0,1,2,3,4]))
print(checkAllElementsEqual([0,0,0,0,0]))
#Output:
False
True
Using all() to Determine if All Elemenets in a List are Equal with Python
The last way to determine if all elements in a list are equal that I want to share with you is uses the all() function.
all() returns True is all values in a list are True, and False is not all values in a list are True.
We can check if all values in a list are equal to the first element and pass this to all()
Below is a Python function which will check if all elements of a list are equal with the all() function.
def checkAllElementsEqual(lst):
return all(x == lst[0] for x in lst)
print(checkAllElementsEqual([0,1,2,3,4]))
print(checkAllElementsEqual([0,0,0,0,0]))
#Output:
False
True
Hopefully this article has been useful for you to check if all elements in a list are equal using Python.