'How do you remove duplicate values in array in Python?

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.



Solution 1:[1]

You could simply use np.unique():

unique_values = np.unique(array)

Solution 2:[2]

You can try to create the array of a set:

deduplicated_array = list(set(array))

Solution 3:[3]

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

deduplicated = list(set(array))

Solution 4:[4]

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/

Solution 5:[5]

This way was worked for me (Ways to remove duplicates from list):

res = []
[res.append(x) for x in test_list if x not in res]

# printing list after removal 
print ("The list after removing duplicates : " + str(res))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hiibb
Solution 2 Benjamin Breton
Solution 3 Numan Ijaz
Solution 4
Solution 5 Shokouh Dareshiri