Python: Check if all the elements in a list are equal

Hello everyone, in this tutorial, we are going to see how we can write a Python program to check if all the elements in a list are equal. We can accomplish this in many ways. A few are listed here.

Method 1: By comparing each element of the list with the first element using a loop

In this method, we store the value of the first element in a variable and then we create a loop to iterate through all the elements so that we can compare each element to the variable storing the first element. If any element in the list is not equal to the first element then we return false and break the loop. See the code implementation in the below program.

def checkList( list):
    first = list[0]
    for elem in list:
        if elem != first:
            return False
            break
    return True
        
list1 = [1,2,3,4,5]
list2 = [1,1,1,1,1]

if checkList(list1):
    print("Elements in list1 are equal")
else:
    print("Elements in list1 are not equal")
    
if checkList(list2):
    print("Elements in list2 are equal")
else:
    print("Elements in list2 are not equal")

Output:

Elements in list1 are not equal
Elements in list2 are equal

Method 2: Using all() method to compare all the elements in the list in a single statement

In this method, the algorithm is the same as above but instead of using a loop we use all() method to compare all the elements with first element. This method returns true if the condition is true for every element of the iterator. See the code.

def checkList( list):
    first = list[0]
    return all(elem == first for elem in list)
    
        
list1 = [1,2,3,4,5]
list2 = [1,1,1,1,1]

if checkList(list1):
    print("Elements in list1 are equal")
else:
    print("Elements in list1 are not equal")
    
if checkList(list2):
    print("Elements in list2 are equal")
else:
    print("Elements in list2 are not equal")

Output:

Elements in list1 are not equal
Elements in list2 are equal

Method 3: Using count() method

In this method, we count the number of elements whose value is equal to the value of the first element in the list. If the count is equal to the length of the list, that means elements are equal otherwise not.

See the code for a better understanding.

def checkList( list):
    first = list[0]
    return list.count(first) == len(list)
    
        
list1 = [1,2,3,4,5]
list2 = [1,1,1,1,1]

if checkList(list1):
    print("Elements in list1 are equal")
else:
    print("Elements in list1 are not equal")
    
if checkList(list2):
    print("Elements in list2 are equal")
else:
    print("Elements in list2 are not equal")

Output:

Elements in list1 are not equal
Elements in list2 are equal

Method 4: Using set() method

In this method, we can use the set() method to convert the list into a set. Now, if all the elements in the list are equal, the set will contain only one element. See the code below.

def checkList( list):
    return len(set(list)) == 1
    
        
list1 = [1,2,3,4,5]
list2 = [1,1,1,1,1]

if checkList(list1):
    print("Elements in list1 are equal")
else:
    print("Elements in list1 are not equal")
    
if checkList(list2):
    print("Elements in list2 are equal")
else:
    print("Elements in list2 are not equal")

Output:

Elements in list1 are not equal
Elements in list2 are equal

Thank you.

Also, read: Find the common elements in two lists in Python

Leave a Reply