New to python-can someone tell me what I am doing wrong?
I need to write a function that takes unknown number of arguments and returns a unique list.
For example:
a= ['mary', 'james', 'john', 'john']
b= ['elsie', 'james', 'elsie', 'james']
unique_list(a,b)
['mary', 'james','john', 'elsie']
This is some of the code I have after doing some research but the output is not what I need:
def unique_list:(*something)
result1= list(something)
result = ' '.join(sum(result1, []))
new= []
for name in result:
if name not in new:
new.append(name)
return new
>>> unique_list(a,b) ['m', 'a', 'r', 'y', ' ', 'j', 'e', 's', 'o', 'h', 'n', 'l', 'i']
This is another one I have tired:
def unique_list(*something):
result= list(something)
new=[]
for name in result:
if name not in new:
new.append(name)
return new
>>> unique_list(a,b) [['mary', 'james', 'john', 'john'], ['elsie', 'james', 'elsie', 'james']]
Another one but I got an error message:
def single_list(*something):
new=[]
for name in something:
if name not in new:
new.append(name)
new2= list(set(new))
return new2
>>> single_list(a,b)
Traceback (most recent call last):
File "", line 1, in
single_list(a,b)
File "", line 6, in single_list
new2= list(set(new))
TypeError: unhashable type: 'list'
Any ideas? Thank you in advance for all your help.
You can concatenate all the
listsand then create asetfrom this resultinglist. This will work for any number of passed in lists where the function looks likedef unique_lists( *lists )