How to remove duplicate elements in list object of multidimensional numpy array in Python

Mulidimensional Numpy Array having duplicate element in List Object

Suppose We have the numpy array of list objects like –

arr=np.array([list(['nan', 'nan']),
        list(['nan', 'nan', 'apple', 'apple', 'banana', 'nan', 'nan']),
        list(['red', 'red']), …,
        list(['nan', 'festival'])], dtype=object)

As, you could see above, in many list object we have duplicate element , we have to remove that , for which we are going to use unique() function.

Python Code to remove duplicate element in list object of numpy Multidimensional Array

We will go to each object , through the loop & will use unique() function to remove duplicates , like below –

import numpy as np

arr=np.array([list(['nan', 'nan']),
       list(['nan', 'nan', 'apple', 'apple', 'banana', 'nan', 'nan']),
       list(['red', 'red']), ...,
       list(['nan', 'festival'])], dtype=object)

finout=[list(np.unique(x)) for x in arr] #going to each object through loop
print(finout)

Code Output, having no duplicate element

When you will run above Python Code , this will give below output –

[['nan'], ['apple', 'banana', 'nan'], ['red'], [Ellipsis], ['festival', 'nan']]

You could see in output, there is no duplicate elements in list object of multidimensional numpy array.