Lists in python - return value once

I would like to make a function that takes a empty list and returns the values only once (even if the same value is entered several times). Example input from user:

list=[a,b,b,c,d,d,d,e,f]

The function returns then:

list=[a,b,c,d,e,f] 

(does not have to be in chronological order, does not matter)

What I have done so far is:

def func1():
    list=[]

3 answers

  • answered 2018-04-14 14:23 Emre

    You just simply make it below

    def func1(mylist):
        return list(set(mylist))
    

    and call your method as :

    newList = func1([1,1,2,2,3,4,5])
    

  • answered 2018-04-14 14:23 Roushan

    you can use set that returns unique value

    In [44]: def func1(l):
        ...:     return [x for x in set(l)]
        ...: 
    
    In [45]:  l=['a','b','b','c','d','d','d','e','f']
        ...: 
    
    In [46]: func1(l)
    Out[46]: ['a', 'c', 'b', 'e', 'd', 'f']
    
    In [47]: 
    

  • answered 2018-04-14 14:23 kvmahesh

    def func1():
    
        list1=[]
        while(1):
            val = input("Enter the value\n")
            if val == 'exit':
                return list(set(list1))
            list1.append(int(val))
    print(func1())