-1

I have a function that finds the unique values of a list, but I can't create a new list with them.

import numpy as np
def unique(list): 
    x = np.array(list) 
    print(np.unique(x))

list1 = [10, 20, 10, 30, 40, 40]  
unique(list1)

My output are the unique values but I want to create a new list containing them. I thought it would be as simple as bellow but when I print list2 the output says "None". I feel there is a really easy answer but I can't figure it out.

list2 = unique(list1) 
print(list2)
eloen
  • 9
  • 5

2 Answers2

1

The result of the function must be returned.

Try this:

import numpy as np
def unique(list):
    x = np.array(list)
    x = np.unique(x)
    return x

list1 = [10, 20, 10, 30, 40, 40]
new_list = unique(list1)
print(new_list)
Higs
  • 384
  • 2
  • 7
0

If order doesn't matter you can use set to remove dupes and then back to a list:

list1 = [10, 20, 10, 30, 40, 40]
list1 = list(set(list1))
list1
[40, 10, 20, 30]
David Erickson
  • 16,433
  • 2
  • 19
  • 35
  • This doesn't address the OP's problem at all, they already have a method to get unique values anyway, showing them another method doesn't address the fundamental issue of their function not returning anything. If they were asking how to remove duplicates from a list it would be a duplicate anyway – juanpa.arrivillaga Oct 19 '20 at 23:47
  • 1
    Thanks this is much simpler and order dosn't matter. – eloen Oct 19 '20 at 23:48