In this article, we will learn to compare two NumPy Arrays elementwise using Python.
Table Of Contents
There are the multiple ways to compare two NumPy Arrays elementwise. Let’s discuss them one by one.
Compare two NumPy Arrays using == operator
When two numpy arrays are compared using == operator, it will return a boolean array. If any value in the boolean array is true, then the corresponding elements in the both the arrays are equal, otherwise not equal.
Approach:
 Import NumPy library.
 Create two numpy arrays of equal length.
 apply the
==
operator on both the arrays, i.e,arr1 ==arr2
. It will return a bool array.  Call the all() function on bool array. If it returns True, it means both arrays are equal, otherwise not.
Source Code
import numpy as np # creating two numpy arrays a = np.array([1, 2, 8, 7, 5]) b = np.array([1, 2, 3, 2, 5]) # comparing the arrays using == operator arr = a==b print(arr) if arr.all(): print('Both arrays are equal') else: print('Both Arrays are not equal')
Output:
[ True True False False True] Both Arrays are not equal
The comparision can aslo be done with greater than (>) and less than (<) operators. The boolean array elements will contain true if the applied operator is true else false.
Code using > and < operator
import numpy as np # creating two numpy arrays a = np.array([1, 2, 8, 7, 5]) b = np.array([1, 2, 8, 7, 5]) # comparing the arrays using > operator print("comparing the arrays using > operator ", a > b) # comparing the arrays using < operator print("comparing the arrays using < operator ", a < b) if ( (~(a < b)).all() and (~(a > b)).all() ): print('Both arrays are equal') else: print('Both Arrays are not equal')
Output:
comparing the arrays using > operator [False False False False False] comparing the arrays using < operator [False False False False False] Both arrays are equal
Compare two NumPy Arrays using for loop and zip()
The zip() method takes multiple iterables as arguments and yeilds nlength tuple. Where n is the number of iterables passed to it. Now using for loop and zip() we will iterate over both the arrays and compare them elementwise.
Approach:
 Import NumPy library.
 Create two numpy arrays of equal length.
 Iterate over array and compare elements
 Print the boolean array.
Source Code
import numpy as np # Creating two numpy arrays a = np.array([1, 2, 8, 7, 5]) b = np.array([1, 2, 3, 4, 5]) # Comparing the arrays using == comparision = [] for i,j in zip(a,b): if i==j: comparision.append(True) else: comparision.append(False) print(comparision) if all(comparision): print('Both arrays are equal') else: print('Both Arrays are not equal')
Output:
[True, True, False, False, True] Both Arrays are not equal
The comparision can aslo be done with greater than (>) and less than (<) operators. By replacing ==
with >
or <
operator.
Compare two NumPy Arrays using for loop
Iterate over the array and compare each element using ==, > or <
operators. For accesing the elements of both the arrays use indexing.
Approach:
 Import NumPy library.
 Create two numpy arrays of equal length.
 Iterate over array using for loop and compare elements
 print the boolean array.
Source Code
import numpy as np # creating two numpy arrays a = np.array([1, 2, 8, 7, 5]) b = np.array([1, 2, 3, 4, 5]) # comparing the arrays using == comparision = [] for i in range(np.size(a)): if a[i]==b[i]: comparision.append(True) else: comparision.append(False) print(" comparision using ==", comparision) if all(comparision): print('Both arrays are equal') else: print('Both Arrays are not equal') # comparing the arrays using > comparision = [] for i in range(np.size(a)): if a[i] > b[i]: comparision.append(True) else: comparision.append(False) print(" comparision using >", comparision) if all(comparision): print('Both arrays are equal') else: print('Both Arrays are not equal') # comparing the arrays using < comparision = [] for i in range(np.size(a)): if a[i] < b[i]: comparision.append(True) else: comparision.append(False) print(" comparision using <", comparision) if all(comparision): print('Both arrays are equal') else: print('Both Arrays are not equal')
Output:
comparision using == [True, True, False, False, True] Both Arrays are not equal comparision using > [False, False, True, True, False] Both Arrays are not equal comparision using < [False, False, False, False, False] Both Arrays are not equal
Compare two NumPy Arrays using List Comprehension
Using list comprehension, iterate over the array and compare each element using ==, > or <
operator.
Approach:
 Import NumPy library.
 Create two numpy arrays of equal length.
 Use list comprehension to compare the elements.
 Print the boolean array.
Source Code
import numpy as np # creating two numpy arrays a = np.array([1, 2, 8, 7, 5]) b = np.array([1, 2, 3, 4, 5]) # comparing the arrays using == comparision = [i==j for i,j in zip(a,b)] if all(comparision): print('Both arrays are equal') else: print('Both Arrays are not equal') # comparing the arrays using > comparision = [i > j for i,j in zip(a,b)] if all(comparision): print('Both arrays are equal') else: print('Both Arrays are not equal') # comparing the arrays using < comparision = [i < j for i,j in zip(a,b)] if all(comparision): print('Both arrays are equal') else: print('Both Arrays are not equal')
Output:
Both Arrays are not equal Both Arrays are not equal Both Arrays are not equal
Compare two NumPy Arrays using while loop
Iterate over the array using while loop and compare each element using ==, > or <
operator. For accesing the elements of both the arrays use indexing.
Approach:
 Import NumPy library.
 Create two numpy arrays of equal length.
 Iterate over array using while loop and compare elements.
 print the boolean array.
Source Code
import numpy as np # creating two numpy arrays a = np.array([1, 2, 8, 7, 5]) b = np.array([1, 2, 3, 4, 5]) # comparing the arrays using == comparision = [] i = 0 while(i < np.size(a)): if a[i]==b[i]: comparision.append(True) else: comparision.append(False) i+=1 print(" comparision using ==", comparision) # comparing the arrays using > comparision = [] i = 0 while(i < np.size(a)): if a[i]==b[i]: comparision.append(True) else: comparision.append(False) i+=1 print(" comparision using >", comparision) # comparing the arrays using < comparision = [] i = 0 while(i < np.size(a)): if a[i]==b[i]: comparision.append(True) else: comparision.append(False) i+=1 print(" comparision using <", comparision)
Output
comparision using == [True, True, False, False, True] comparision using > [True, True, False, False, True] comparision using < [True, True, False, False, True]
Summary
Great! you made it, We have discussed All possible methods to compare two NumPy Arrays elementwise using Python. Happy learning.
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.