Best way to make a list unique?
maxm at mxm.dk
Tue Mar 8 13:09:07 CET 2005
Eric Pederson wrote:
> I have
> and I want a list of only the unique members.
> This seems inefficient, but works fine over my small sample lists:
>>>>listA=[a for a in set(listA)]
> Is there a more efficient approach for cases where listA is large?
no. Even though the code can be a little simpler:
listA = list(Set(listA))
You don't even need to convert it to a list. You can just iterate over
>>> la = [1,2,3,4,3,2,3,4,5]
>>> from sets import Set
>>> sa = Set(la)
>>> for itm in sa:
... print itm
hilsen/regards Max M, Denmark
IT's Mad Science
More information about the Python-list