Python : Check if all elements in a List are same or matches a condition

In this article we will dicuss different ways to check if all element in a given List are same or matches a condition.

Suppose we have a list of string i.e.

# List of string 
listOfStrings = ['Hello'] * 10

Now let’s use python all() function to check if all elements in the given list are same.

Python : all() function

Python all() function checks if all Elements of given Iterable is True.

check if element are same using all()

Let’s convert the list to Iterable and check if each entry of iterable is equal to first element of list using all() i.e.

'''    
    check if element are same using all()
    It will Iterate through all the elements in list and check if all elements are similar to first element or not.
'''
result = False;
if len(listOfStrings) > 0 :
    result = all(elem == listOfStrings[0] for elem in listOfStrings)

if result :
    print("All Elements in List are Equal")
else:        
    print("All Elements in List are Not Equal")

Check if all elements are same using list.count()

count() returns the occurrence count of given element in the list.

Let’s call the count() function of list with firts element of list as argument. If its occurrence count is equal to the length of list, then it means all elements in list are Same i.e.

'''    
    check if element are same using list.count()
    If occurence count of first element in list is equal to length of list.
    Then it means all elements in List are equal 
'''
result = False;
if len(listOfStrings) > 0 :
    result = listOfStrings.count(listOfStrings[0]) == len(listOfStrings)

Let’s do the same thing in single line i.e.

result = len(listOfStrings) > 0 and all(elem == listOfStrings[0] for elem in listOfStrings)

Check if all elements are same using Set

As set contains only unique elements, so convert the list to set. If set size is 1 then it means all elements in given list are same i.e.

'''    
    As set contains unique elements only, so if list has similar elements, then only one will stored in set.
'''    
result = len(set(listOfStrings)) == 1

Complete example is as follows,

def main():
    
    # List of string 
    listOfStrings = ['Hello'] * 10
    
    # Print the List
    print(listOfStrings)
    
    '''    
        check if element are same using all()
        It will Iterate through all the elements in list and check if all elements are similar to first element or not.
    '''
    result = False;
    if len(listOfStrings) > 0 :
        result = all(elem == listOfStrings[0] for elem in listOfStrings)    
    
    if result :
        print("All Elements in List are Equal")
    else:        
        print("All Elements in List are Not Equal")
    
    '''    
        check if element are same using list.count()
        If occurence count of first element in list is equal to length of list.
        Then it means all elements in List are equal 
    '''
    result = False;
    if len(listOfStrings) > 0 :
        result = listOfStrings.count(listOfStrings[0]) == len(listOfStrings)
        
    if result :
        print("All Elements in List are Equal")
    else:        
        print("All Elements in List are Not Equal")
        
        
    # Do the above logic in single line    
    result = len(listOfStrings) > 0 and all(elem == listOfStrings[0] for elem in listOfStrings) 
       
    if result :
        print("All Elements in List are Equal")
    else:        
        print("All Elements in List are Not Equal")      
    
    
    '''    
        As set contains unique elements only, so if list has similar elements, then only one will stored in set.
    '''    
    result = len(set(listOfStrings)) == 1   
    
    if result :
        print("All Elements in List are Equal")
    else:        
        print("All Elements in List are Not Equal")      
         
if __name__ == '__main__':
    main()

Output:

['Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello']
All Elements in List are Equal
All Elements in List are Equal
All Elements in List are Equal
All Elements in List are Equal

 

 

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