Python: Check if all values are same in a Numpy Array (both 1D and 2D)

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,

import numpy as np

Now suppose we have a 1D Numpy array,

# create a 1D numpy array from a list
arr = np.array([9, 9, 9, 9, 9, 9])

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

# Check all values in an array are equal to its first element
result = np.all(arr == arr[0])

if result:
    print('All Values in Array are same / equal')
else:
    print('All Values in Array are not same')

Output:

All Values in Array are same / equal

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,

bool_arr = (arr == arr[0])

print(bool_arr)

Output:

[True True True True True True]

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,

result = np.all(bool_arr)

print(result)

Output:

True

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,

# create a 1D numpy array from a list
arr = np.array([9, 9, 9, 9, 9, 9])

# Check if all items in an array are equal
result = np.max(arr) == np.min(arr)

if result:
    print('All Values in Array are same / equal')
else:
    print('All Values in Array are not same')

Output:

All Values in Array are same / equal

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,

arr_2d = np.array([[3, 3, 3, 3],
                   [3, 3, 3, 3],
                   [3, 3, 3, 3],
                   [3, 3, 3, 3]])

# Get a flattened 1D view of 2D numpy array
flatten_arr = np.ravel(arr_2d)

# Check if all value in 2D array are equal
result = np.all(arr_2d==flatten_arr[0])

if result:
    print('All Values in 2D Numpy Array are same / equal')
else:
    print('All Values in 2D Numpy Array are not same')

Output:

All Values in 2D Numpy Array are same / equal

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,

arr_2d = np.array([[3, 3, 3, 3],
                   [3, 3, 0, 3],
                   [3, 3, 0, 3],
                   [3, 3, 3, 3]])

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

# Check rows in which all values are equal
for i in range(arr_2d.shape[0]):
    if np.all(arr_2d[i]==arr_2d[i][0]):
        print('Row: ', i)

Output:

Row:  0
Row:  3

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

# Check Columns in which all values are equal
trans_arr = arr_2d.T
for i in range(trans_arr.shape[0]):
    if np.all(trans_arr[i] == trans_arr[i][0]):
        print('Column: ', i)

Output:

Column:  0
Column:  1
Column:  3

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,

import numpy as np


def main():

    print('**** Check if all elements are equal in a 1D Numpy Array ****')

    # create a 1D numpy array from a list
    arr = np.array([9, 9, 9, 9, 9, 9])

    print('1D Numpy Array:')
    print(arr)

    # Check all values in an array are equal to its first element
    result = np.all(arr == arr[0])

    if result:
        print('All Values in Array are same / equal')
    else:
        print('All Values in Array are not same')

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

    # Check if all items in an array are equal
    result = np.max(arr) == np.min(arr)

    if result:
        print('All Values in Array are same / equal')
    else:
        print('All Values in Array are not same')

    print('**** Check if all elements are equal in a 2D Numpy Array or Matrix ****')

    arr_2d = np.array([[3, 3, 3, 3],
                       [3, 3, 3, 3],
                       [3, 3, 3, 3],
                       [3, 3, 3, 3]])

    # Get a flattened 1D view of 2D numpy array
    flatten_arr = np.ravel(arr_2d)

    # Check if all value in 2D array are equal
    result = np.all(arr_2d==flatten_arr[0])

    if result:
        print('All Values in 2D Numpy Array are same / equal')
    else:
        print('All Values in 2D Numpy Array are not same')

    arr_2d = np.array([[3, 3, 3, 3],
                       [3, 3, 0, 3],
                       [3, 3, 0, 3],
                       [3, 3, 3, 3]])

    print('*** Find rows wih same values in a matrix or 2D Numpy array***')

    # Check rows in which all values are equal
    for i in range(arr_2d.shape[0]):
        if np.all(arr_2d[i]==arr_2d[i][0]):
            print('Row: ', i)



    print('*** Find columns wih same values in a matrix or 2D Numpy array***')

    # Check Columns in which all values are equal
    trans_arr = arr_2d.T
    for i in range(trans_arr.shape[0]):
        if np.all(trans_arr[i] == trans_arr[i][0]):
            print('Column: ', i)


if __name__ == '__main__':
    main()

Output:

**** Check if all elements are equal in a 1D Numpy Array ****
1D Numpy Array:
[9 9 9 9 9 9]
All Values in Array are same / equal
**** Check if all elements are equal in a 1D Numpy Array using min() & max() ****
All Values in Array are same / equal
**** Check if all elements are equal in a 2D Numpy Array or Matrix ****
All Values in 2D Numpy Array are same / equal
*** Find rows with same values in a matrix or 2D Numpy array***
Row:  0
Row:  3
*** Find columns with same values in a matrix or 2D Numpy array***
Column:  0
Column:  1
Column:  3

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top