Check if all Elements in Array are Equal in Python : 3 Methods

Check if all Elements in Array are Equal in Python

The array allows you to store variables of the same type in a single variable. Do you want to check if all elements in the array are equal in Python or not? In this post, you will know how to check equality using various methods.

Methods to check check if all elements in the array are equal in Python

Let’s know all the methods that you will use to check whether all the elements of an array are equal or not.

Method 1: Using the all() function

In this method, you will use the all() function with a comparison of the first element. It will take the first element and check it will all the other elements.

Run the below lines of code and check the equality of the elements.

sample_array =[10,10,10,10,10]
result = all(element == sample_array[0] for element in sample_array)
print(result)

Output

True
sample_array =[10,10,10,20,10]
result = all(element == sample_array[0] for element in sample_array)
print(result)

Output

False

Method 2:  Using the count() function

The other method to check if all elements in the array are equal in Python or not is the use of the count() function. The below code will check if the count of the first element of the array is the same as the length of the array or not. If it’s not then will return False.

sample_array =[10,10,10,10,10]
result = sample_array.count(sample_array[0]) == len(sample_array)
print(result)

Output

True
sample_array =[10,10,10,20,10]
result = sample_array.count(sample_array[0]) == len(sample_array)
print(result)

Output

False

Method 3: Check the equality of elements using the set() function

Python provides you with the set() function that finds all the unique elements of the array and ignores the duplicates. In this example, you will first convert the array to the set() function. After that using the len() function check the length of the set and if it is equal to 1 then all the elements of the array are equal.

sample_array =[10,10,10,10,10]
result = len(set(sample_array)) == 1
print(result)

Output

True
sample_array =[10,10,10,20,10]
result = len(set(sample_array)) == 1
print(result)

Output

False

Conclusion

Sometimes you have to check if all elements in the array are equal in Python or not. The above methods use all(), count(), and set() functions to achieve the task. You can use any one of them.

I hope you have liked this tutorial. If you have any doubts or queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner