get rid of duplicate elements in list without set

Albert Hopkins marduk at letterboxes.org
Fri Mar 20 10:30:11 EDT 2009


On Fri, 2009-03-20 at 07:16 -0700, Alexzive wrote:
> Hello there,
> 
> I'd like to get the same result of set() but getting an indexable
> object.
> How to get this in an efficient way?
> 
> Example using set
> 
> A = [1, 2, 2 ,2 , 3 ,4]
> B= set(A)
> B = ([1, 2, 3, 4])
> 
>  B[2]
> TypeError: unindexable object

>>> A = [1, 2, 2 ,2 , 3, 4]
>>> B = list(set(A))
>>> B[2]
3

However, as sets are unordered, there is no guarantee that B will have
the same ordering as A.






More information about the Python-list mailing list