• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Check if All Elements in Array are Equal in Python

Check if All Elements in Array are Equal in Python

February 25, 2022 Leave a Comment

With Python, we can check if all elements in a list are equal by converting the list to a set and checking if the set has length 1.

def checkAllElementsEqual(lst):
    return len(set(lst)) == 1

print(checkAllElementsEqual([0,1,2,3,4]))
print(checkAllElementsEqual([0,0,0,0,0]))

#Output:
False
True

When working with collections of data in a Python program, it’s possible you want to check if all elements in an array are equal.

Arrays in Python are called lists, and we can easily check if all elements in a list are equal.

To check if all items in a list are equal, the easily way is to convert the list to a set and check the set’s length. If the length of the set is 1, then we know that all elements are equal.

Below is a simple function in Python of how to check if all elements of a list are the same.

def checkAllElementsEqual(lst):
    return len(set(lst)) == 1

print(checkAllElementsEqual([0,1,2,3,4]))
print(checkAllElementsEqual([0,0,0,0,0]))

#Output:
False
True

There are a few other ways you can check if all elements in a list are equal in Python which you can read about below.

Using a Loop to Check if All Elements in a List are the Same in Python

Another method we can use to check if all elements in a list are the same is to use a for loop.

To check if all items in a list are equal with a loop, we just check if all elements are equal to the first element.

Below is a Python function which will check if all elements of a list are equal with a for loop.

def checkAllElementsEqual(lst):
    for x in lst:
        if lst[0] != x:
            return False
    return True

print(checkAllElementsEqual([0,1,2,3,4]))
print(checkAllElementsEqual([0,0,0,0,0]))

#Output:
False
True

Using count() to Check if All Items in a List are the Same in Python

Another way we can check if all items in a list are equal is with the help of the count() function.

The count() function in Python gives us the count of how many times a particular value is found in a list.

If a list has all equal values, then the count of the first value should equal the length of the list.

Below is how to check if all elements in a list are equal with the help of the count() function.

def checkAllElementsEqual(lst):
    return lst.count(lst[0]) == len(lst)

print(checkAllElementsEqual([0,1,2,3,4]))
print(checkAllElementsEqual([0,0,0,0,0]))

#Output:
False
True

Using all() to Determine if All Elemenets in a List are Equal with Python

The last way to determine if all elements in a list are equal that I want to share with you is uses the all() function.

all() returns True is all values in a list are True, and False is not all values in a list are True.

We can check if all values in a list are equal to the first element and pass this to all()

Below is a Python function which will check if all elements of a list are equal with the all() function.

def checkAllElementsEqual(lst):
    return all(x == lst[0] for x in lst)

print(checkAllElementsEqual([0,1,2,3,4]))
print(checkAllElementsEqual([0,0,0,0,0]))

#Output:
False
True

Hopefully this article has been useful for you to check if all elements in a list are equal using Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Find Second Smallest Value in List
  • 2.  Using Python to Count Number of False in List
  • 3.  Sort List of Tuples in Python
  • 4.  Inverting Dictionary Variables in Python with Dictionary Comprehension
  • 5.  Python Replace Space with Underscore Using String replace() Function
  • 6.  Using Python to Find Closest Value in List
  • 7.  Using Python to Split a Number into Digits
  • 8.  Not Equal Operator != in Python
  • 9.  How to Check if String Contains Uppercase Letters in Python
  • 10.  Using Python to Sort Two Lists Together

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy