w3resource logo
Python Exercises

Python NumPy: Get the unique elements of an array

Python NumPy: Array Object Exercise-19 with Solution

Write a Python program to get the unique elements of an array.

Sample Solution:-

Python Code:

import numpy as np
x = np.array([10, 10, 20, 20, 30, 30])
print("Original array:")
print(x)
print("Unique elements of the above array:")
print(np.unique(x))
x = np.array([[1, 1], [2, 3]])
print("Original array:")
print(x)
print("Unique elements of the above array:")
print(np.unique(x))

Sample Output:

Original array:                                                        
[10 10 20 20 30 30]                                                    
Unique elements of the above array:                                    
[10 20 30]                                                             
Original array:                                                        
[[1 1]                                                                 
 [2 3]]                                                                
Unique elements of the above array:                                    
[1 2 3]

Python Code Editor:

import numpy as np
x = np.array([10, 10, 20, 20, 30, 30])
print("Original array:")
print(x)
print("Unique elements of the above array:")
print(np.unique(x))
x = np.array([[1, 1], [2, 3]])
print("Original array:")
print(x)
print("Unique elements of the above array:")
print(np.unique(x))

Improve this sample solution and post your code through Disqus