Home > Net >  How do you remove duplicate values in array in Python?
How do you remove duplicate values in array in Python?

Time:03-22

I have an array where each element is the mean of a set of random numbers. I want to be able to remove duplicate values in this array. How would I go about doing this?

So far I have tried:

for i in range(len(array)):
    
    for j in array:

        if array[i] == j:

and then some operation to remove the element from the array. However, this will just remove every instance of every duplicated element.

CodePudding user response:

You can try to create the array of a set:

deduplicated_array = list(set(array))

CodePudding user response:

If you don't care about the order of the elements then use the following

deduplicated = list(set(array))

CodePudding user response:

You could simply use np.unique():

unique_values = np.unique(array)

CodePudding user response:

You may want to look into using a python set :)

Example with code:

# set cannot have duplicates
# Output: {1, 2, 3, 4}
my_set = {1, 2, 3, 4, 3, 2}
print(my_set)

# we can make set from a list: what you want
# Output: {1, 2, 3}
my_set = set([1, 2, 3, 2])
print(my_set)

Then you can use remove or discard (if you don't want errors upon removing non-existing items):

my_set = {1, 3, 4, 5, 6}
print(my_set)

# discard an element
# Output: {1, 3, 5, 6}
my_set.discard(4)
print(my_set)

# remove an element
# Output: {1, 3, 5}
my_set.remove(6)
print(my_set)

# discard an element
# not present in my_set
# Output: {1, 3, 5}
my_set.discard(2)
print(my_set)

# remove an element
# not present in my_set
# you will get an error.
# Output: KeyError

my_set.remove(2)

If you prefer to work with a list, then you can then convert back to a list with the list function:

my_list = list(my_set)

Docs: https://python-reference.readthedocs.io/en/latest/docs/sets/

  • Related