Check if all elements of List are equal in Python

In this article, we will discuss different ways to check if all elements in a List are same or not in Python.

Table Of Contents

Check if all elements of List are equal using a for loop

Iterate over all the elements of list using a for loop, and during iteration check if all elements are equal to the first element of list or not. If yes, then it means all elements of list are same. But before that, check if list has atleast some elements.

Let’s see an example, where we will check if all numbers in a list are equal.

listOfNumbers = [14, 14, 14, 14, 14]

# Check if all elements of list are equal and 
# list has atleast some elements
def isSame(listOfElements):
    result = len(listOfElements) > 0
    if result:
        for elem in listOfElements:
            if elem != listOfElements[0]:
                result = False
                break
    return result

if isSame(listOfNumbers):
    print('All elements in list are equal')
else:
    print('All elements in list are not equal')

Output:

Advertisements
All elements in list are equal

Check if all elements of List are equal using all() function

Iterate over all the elements of list and check if each element is equal to the first element of list or not. Store the results in a boolean list. Size of this boolean list will be equal to the size of the original list. Also, a True value in this boolean list tells that the corresponding value in original list is equal to the first value of list. So, if all elements in this boolean list are True, then it means all elements in original list are equal. To check if all elements of a list are True, we can use the all() function. It accepts a sequence of boolean type elements and returns True if all the elements in that sequence evaluates to True.

Let’s see an example, where we will check if all numbers in a list are equal.

listOfNumbers = [14, 14, 14, 14, 14]

# Check if all elements of list are equal and 
# list has atleast some elements
if len(listOfNumbers) > 0 and all(elem == listOfNumbers[0] for elem in listOfNumbers):
    print('All elements in list are equal')
else:
    print('All elements in list are not equal')

Output:

All elements in list are equal

Check if all elements of List are equal using count() function

Fetch the first value of list and then find its occurrence count in the list. If that is equal to the length of the list, then it means all elements in list are equal. Let’s see an example, where we will check if all numbers in a list are equal.

listOfNumbers = [14, 14, 14, 14, 14]

# Get number of elements in list
numberOfElements = len(listOfNumbers)

# Check if all elements of list are equal and 
# list has atleast some elements
if numberOfElements > 0 and numberOfElements == listOfNumbers.count(listOfNumbers[0]):
    print('All elements in list are equal')
else:
    print('All elements in list are not equal')

Output:

All elements in list are equal

Check if all elements of List are equal using Set

Set in Python contains only unique elements. So, create a set from the list. If the size of set is 1, then it means all elements in list are equal. Let’s see an example, where we will check if all numbers in a list are equal.

listOfNumbers = [14, 14, 14, 14, 14]

# Check if all elements of list are equal
if len(set(listOfNumbers)) == 1:
    print('All elements in list are equal')
else:
    print('All elements in list are not equal')

Output:

All elements in list are equal

Check if all elements of List are equal using NumPy Array

Create a NumPy Array from list. Then match all elements of array with the first element of list. If the resultant boolean array contains only True, then it means all elements in list are equal. Let’s see an example,

import numpy as np

listOfNumbers = [14, 14, 14, 14, 14]

# Create a numpy array from list
arr = np.array(listOfNumbers)

# Check if all elements of list are equal
if arr.size > 0 and (arr == listOfNumbers[0]).all():
    print('All elements in list are equal')
else:
    print('All elements in list are not equal')

Output:

All elements in list are equal

Summary

We learned how to check if all elements in a List are equal or not in Python. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top