Remove duplicate items from a list using set
A common problem beginners face is to remove duplicate items from a list. In Python it can be done easily. First make a set from the list and then make a list for that set.
Here is a python example:
Let me know if you use any different technique to remove duplicates from a list.
myList = list(set(myList))
Here is a python example:
>>> myList = [1, 2, 3, 3, 2, 2, 4, 5, 5]
>>> myList
[1, 2, 3, 3, 2, 2, 4, 5, 5]
>>> myList = list(set(myList))
>>> myList
[1, 2, 3, 4, 5]
>>>
Let me know if you use any different technique to remove duplicates from a list.
Comments
>>> myList
[1, 2, 3, 3, 2, 2, 4, 5, 5]
>>> myList = dict.fromkeys(myList)
>>> myList
{1: None, 2: None, 3: None, 4: None, 5: None}
>>> myList = myList.keys()
>>> myList
[1, 2, 3, 4, 5]
>>>
>>> mylist = [1, 2, 3, 3, 2, 2, 4, 5, 5]
>>> outlist = []
>>> for element in mylist:
if element not in outlist:
outlist.append(element)
>>> outlist
[1, 2, 3, 4, 5]
>>>
filter(
lambda e: e not in myList,
myList
)
>>> mylist=[1,2,3,3,2,2,4,5,5]
>>> outlist=[]
>>> for element in mylist:
if mylist.count(element)==1:
outlist.append(element)
>>> outlist
[1,2,3,4,5]
look at the link
http://www.peterbe.com/plog/uniqifiers-benchmark
but actually that's the easy part
because it is far more difficult to unique elements in a sequence and preserve them each part in a separate list or any other data type
Any idea anyone?
[ mylist[i] for i,x in enumerate(mylist) if x not in mylist[i+1:]]
[ mylist[i] for i,x in enumerate(mylist) if x not in mylist[i+1:]]
P.S: I'm totally a beginner :)
http://www.peterbe.com/plog/uniqifiers-benchmark
especially function 8 (f8)
"filter(lambda e: e not in myList, myList)"
returns an empty list!