0

How to create a function that gets a list filled with strings as an input and then deletes the duplicates

16th Nov 2016, 3:29 PM
Tolga
Tolga - avatar
8 Antworten
+ 1
ok, another way: def uniqList(lst): newList = list() for a in lst: if a not in newList: newList.append(a) return newList if you want to update the old list, just write: myList = uniqList(myList) with this function or previous
16th Nov 2016, 4:38 PM
Demeth
Demeth - avatar
0
you can transform your list into set, and then back to list, this will remove duplicates def uniqList (lst): return list(set(lst))
16th Nov 2016, 4:23 PM
Demeth
Demeth - avatar
0
i was kinda looking for another way to do it
16th Nov 2016, 4:31 PM
Tolga
Tolga - avatar
0
it works when i dont make the code get the list as an input but when i do get the list as an input the function splits the list into characters and prints something like ["[","a",",","b","c","]"]
16th Nov 2016, 4:50 PM
Tolga
Tolga - avatar
0
do you really giving a list as function argument? what you write seems to be a string representation of printed list, but it's not a list. function does what it does but with chars in this string. you must look for mistakes in you list assigning, maybe it's really a string
16th Nov 2016, 4:56 PM
Demeth
Demeth - avatar
0
Just checked the type and yes it sees it as a string
16th Nov 2016, 5:07 PM
Tolga
Tolga - avatar
0
so the i have a listN=input ("enter your list: ") at start and i give an input like ["abc","bcd","abc","bcd"] what can i do to make it see this input as a list
16th Nov 2016, 5:15 PM
Tolga
Tolga - avatar
0
first, remember this: all you input is a string, so you must convert it to what you need, unless you need a string. it's easier with numbers, you just use int(input()) or float(input()). to input a list (of strings, in your case), you must either do it in a loop, appending each input to a list, or input a whole string, dividing elements with specific separator that your strings-elements do not contain. listN = input() #here it is a string, for example, "abc, def, ghi" listN = listN.split(", ") #and here it is a list ["abc", "def", "ghi"] then you can do anything you need
16th Nov 2016, 7:14 PM
Demeth
Demeth - avatar