Check if all elements in a list are equal in Python

avatar

Borislav Hadzhiev

Last updated: Aug 11, 2022

banner

Photo from Unsplash

Check if all elements in a list are equal in Python #

To check if all elements in a list are equal:

  1. Use the set() class to convert the list to a set object.
  2. Check if the length of the set is equal to 1.
  3. If the length of the set is equal to 1, then all elements in the list are equal.
main.py
my_list = ['a', 'a', 'a', 'a'] if len(set(my_list)) == 1: print('All elements in list are equal') else: print('Not all elements in list are equal') # 👇️ if you consider an empty list to have all equal elements my_empty_list = [] if len(set(my_empty_list)) <= 1: print('All elements in list are equal') else: print('Not all elements in list are equal')

We used the set() class to convert the list to a set object.

Set objects are an unordered collection of unique elements, so by converting the list to a set, all duplicate values get removed.

If all elements in the list are equal, the set will only contain 1 value.

If you consider an empty list one where all elements are equal, use the less than or equals comparison operator (<=).

Alternatively, you can use the list.count() method.

To check if all elements in a list are equal:

  1. Pass the first list item to the list.count() method.
  2. If the number of occurrences of the first list item is equal to the list's length, all elements in the list are equal.
main.py
my_list = ['a', 'a', 'a', 'a'] if my_list.count(my_list[0]) == len(my_list): print('All elements in list are equal') else: print('Not all elements in list are equal') # 👇️ if you consider an empty list to have all equal elements if not my_list or my_list.count(my_list[0]) == len(my_list): print('All elements in list are equal') else: print('Not all elements in list are equal')

We used the list.count() method to count the number of occurrences of the first list item in the list.

The list.count() method takes a value and returns the number of times the provided value appears in the list.

If the number of times the first list item appears in the list is equal to the list's length, then all elements in the list are equal.

If you consider an empty list one where all elements are equal, check if the list is falsy. Empty sequences, e.g. empty string, tuple and list are all falsy values.

Alternatively, you can use the all() function.

Use the all() function to check if all elements in a list are equal, e.g. if all(item == my_list[0] for item in my_list):. The all() function will return True if all of the list's elements are equal and False otherwise.

main.py
my_list = ['a', 'a', 'a', 'a'] if all(item == my_list[0] for item in my_list): # 👇️ this runs print('All elements in list are equal') else: print('Not all elements in list are equal')

We used a generator expression to iterate over the list.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current list item is equal to the first list item.

The all() built-in function takes an iterable as an argument and returns True if all elements in the iterable are truthy (or the iterable is empty).

If any of the list items is not equal to the first list item, the all() function will short-circuit returning False.

The all() function will return True for any condition if the list is empty.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.