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.

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

Dustin said…
I've always done it like this:

>>> 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]
>>>
Paddy3118 said…
Order preserving

>>> 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]
>>>
Tamim Shahriar said…
Thanks to both of you. Using dictionary is interesting.
Sergei Lebedev said…
Variation of the last method:

filter(
lambda e: e not in myList,
myList
)
Anonymous said…
boring but it works

>>> 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]
Anonymous said…
Sorry cancel that - I got confused lol that only returns items that appear once !
giannisfs said…
i found these
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?
Tyler Crompton said…
Good post. By the way, don't user dictionaries for this. Sets are basically dictionaries without associated values. It will be faster to use sets than dictionaries since you are instantiating objects you don't need. Plus, the extra function call to the static method fromkeys adds a tiny fragment.
Saurabh said…
mylist =
[ mylist[i] for i,x in enumerate(mylist) if x not in mylist[i+1:]]
Saurabh said…
mylist =
[ mylist[i] for i,x in enumerate(mylist) if x not in mylist[i+1:]]
Karim Nasr said…
Thanks to you all , but I'm curious about the set method by Tamim Shahriar, this method isn't preserve order , is there any way to make it preserve order ?

P.S: I'm totally a beginner :)
giannisfs said…
Take a look at --->
http://www.peterbe.com/plog/uniqifiers-benchmark

especially function 8 (f8)
Unknown said…
http://love-python.blogspot.com/2008/09/remove-duplicate-items-from-list-using.html?showComment=1237762920000#c8641189374003493307
"filter(lambda e: e not in myList, myList)"
returns an empty list!

Popular posts from this blog

Strip HTML tags using Python

lambda magic to find prime numbers

Convert text to ASCII and ASCII to text - Python code