Open In App

numpy.setdiff1d() function in Python

Last Updated : 17 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

numpy.setdiff1d() function find the set difference of two arrays and return the unique values in arr1 that are not in arr2.

Syntax : numpy.setdiff1d(arr1, arr2, assume_unique = False)

Parameters :
arr1 : [array_like] Input array.
arr2 : [array_like] Input comparison array.
assume_unique : [bool] If True, the input arrays are both assumed to be unique, which can speed up the calculation. Default is False.

Return : [ndarray] 1D array of values in arr1 that are not in arr2. The result is sorted when assume_unique = False, but otherwise only sorted if the input is sorted.

Code #1 :




# Python program explaining
# numpy.setdiff1d() function
    
# importing numpy as geek 
import numpy as geek 
   
arr1 = [5, 6, 2, 3, 4]
arr2 = [1, 2, 3]
   
gfg = geek.setdiff1d(arr1, arr2)
   
print (gfg)


Output :

[4 5 6]

 
Code #2 :




# Python program explaining
# numpy.setdiff1d() function
    
# importing numpy as geek 
import numpy as geek 
   
arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr2 = [1, 3, 5, 7, 9, 11, 13, 15]
   
gfg = geek.setdiff1d(arr1, arr2)
   
print (gfg)


Output :

[2 4 6 8]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads