w3resource logo
Python Exercises

Python NumPy: Find common values between two arrays

Python NumPy: Array Object Exercise-18 with Solution

Write a Python program to find common values between two arrays.

Sample Solution:-

Python Code:

import numpy as np
array1 = np.array([0, 10, 20, 40, 60])
print("Array1: ",array1)
array2 = [10, 30, 40]
print("Array2: ",array2)
print("Common values between two arrays:")
print(np.intersect1d(array1, array2))

Sample Output:

Array1:  [ 0 10 20 40 60]                                              
Array2:  [10, 30, 40]                                                  
Common values between two arrays:                                      
[10 40]

Python Code Editor:

import numpy as np
array1 = np.array([0, 10, 20, 40, 60])
print("Array1: ",array1)
array2 = [10, 30, 40]
print("Array2: ",array2)
print("Common values between two arrays:")
print(np.intersect1d(array1, array2))

Improve this sample solution and post your code through Disqus