Best way to make a list unique?

Scott David Daniels Scott.Daniels at Acm.Org
Tue Mar 8 08:01:15 EST 2005


Max M wrote:
> Eric Pederson wrote:
> listA = list(Set(listA))
As of 2.4, set is a built-in type (2.3 had Set in module sets).
Another 2.4-ism is "sorted", which might very well be the way you
want to turn the set into a list:

     listA = sorted(set(listA))

for this particular use, you can define sorted in 2.3 as:

     def sorted(iterable):
         result = list(iterable)
         result.sort()
         return result

The full sorted functionality can be (and has been) defined in 2.3,
but just using 2.4 would be a better bet.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list