This post will discuss how to check if all elements in a list are identical in Python.

1. Using Set

A simple and fairly efficient solution is to convert the list to a set. Since the set data structure does not permit duplicates, the resultant set’s length would be 1 when all the list elements are equal.

Download  Run Code

2. Using count() function

Another option is to get a count of any arbitrary member of the list. If the count is the same as the length of the list, you can say that all the list elements are equal. Note this function will fail with an empty list.

Download  Run Code

3. Using all() function

Another plausible way is to use the all() function, which returns True if all elements of the iterable are true. This can be used as follows:

Download  Run Code

4. Using slicing

Here’s a tricky solution using slicing:

Download  Run Code

5. Using NumPy

Numpy also offers the all() function, which can be used with the diff() function, as shown below:

 
You can also use the allclose() function to check if all elements in the list equal to the first element.

That’s all about determining whether all items in a list are identical in Python.