A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2005-September/339002.html below:

Removing duplicates from a list

Removing duplicates from a list Removing duplicates from a listprzemek drochomirecki drochom at WYTNIJ.hyene.com
Wed Sep 14 18:40:35 EDT 2005
> I've a list with duplicate members and I need to make each entry
> unique.
>
> I've come up with two ways of doing it and I'd like some input on what
> would be considered more pythonic (or at least best practice).
>
> Method 1 (the traditional approach)
>
> for x in mylist:
>     if mylist.count(x) > 1:
>         mylist.remove(x)
>
> Method 2 (not so traditional)
>
> mylist = set(mylist)
> mylist = list(mylist)
>
> Converting to a set drops all the duplicates and converting back to a
> list, well, gets it back to a list which is what I want.
>
> I can't imagine one being much faster than the other except in the case
> of a huge list and mine's going to typically have less than 1000
> elements.
>
> What do you think?
>
> Cheers,
>
> Robin
>

Hi,

Try this:

def unique(s):
 e = {}
 for x in s:
  if not e.has_key(x):
   e[x] = 1
 return e.keys()

Regards
Przemek



More information about the Python-list mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4