In this article we will discuss different ways to check if all values in a 1D or 2D numpy array are equal. Then we will see how to find rows or columns with the same values in a 2D array or matrix.

Check if all elements are equal in a 1D Numpy Array using numpy.all()

First of all we will import the numpy module,

Now suppose we have a 1D Numpy array,

Let’s check if all items are equal in this array,

Output:

This confirms that all values in the array are the same. But what just happened in this single line?
How did it work?

First we compared all the values in array with the first element of array,

Output:

It compares the first element of the array with all the other elements in the array and returns a bool array of the same size. Each element in this bool array corresponds to an element in the main array, if an element is equal to the first element of the array then the corresponding value in the bool array will be True else it will be False,

Output:

If all elements in this bool array are True, then it means all values in the main array are equal.

Check if all elements are equal in a 1D Numpy Array using min() & max()

If we have an array of integer type, them there is an another simple way to check if all elements in the array are equal,

Output:

As our numpy array contains only integers, so if the minimum value in array is equal to the maximum value in array, then it means all values in the array are the same.

Check if all elements are equal in a Multidimensional Numpy Array or Matrix

If we have a 1D array then it is easy to select an individual element of the array for comparison. But if we have multi dimensional array like 2D or 3D array, then for each type of array there is different technique, like to select first element from a 2D numpy array it is arr[0][0], whereas for a 3D array it is arr[0][0][0].

So, let’s create a generic solution that should work with an array of any dimension and confirms if all values are equal or not,

Output:

It confirms that all the values in the 2D numpy array are the same.

How did it work?

numpy.ravel() returns a flattened 1D view of the input array. Then we selected the first element in this array and compared it with all the other elements of 2D numpy array, to check if all values are the same or not.

Find rows with same values in a matrix or 2D Numpy array

Suppose we have a 2D numpy array or matrix,

Now we want to find all rows and columns which contain the same values. Let’s see how to do that,

Find rows with same values in a matrix or 2D Numpy array

Output:

We iterated over each row of the 2D numpy array and for each row we checked if all elements are equal or not by comparing all items in that row with the first element of the row.

Find columns with same values in a matrix or 2D Numpy array

Output:

We iterated over each row of the 2D numpy array and for each row we checked if all elements are equal or not by comparing all items in that row with the first element of the row.

The complete example is as follows,

Output:

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.