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:
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

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
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 soughtafter 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.