get rid of duplicate elements in list without set

Michael Spencer mahs at telcopartners.com
Fri Mar 20 23:07:24 CET 2009


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
> 
> Many thanks, alex
> --
> http://mail.python.org/mailman/listinfo/python-list
> 
Provided your list items are hashable, you could use a set to keep track of what 
you've seen:

  >>> A = [1, 2, 2 ,2 , 3 ,4]
  ...
  >>> seen=set()
  ...
  >>> B=[]
  >>> for item in A:
  ...     if not item in seen:
  ...         B.append(item)
  ...         seen.add(item)
  ...
  >>> B
  [1, 2, 3, 4]

And, if you really want, you can get the body of this into 1-line, noting that 
seen.add returns None, so the expression (item in seen or seen.add(item)) 
evaluates to True if item is in seen, or None (and item is added to seen) if not.

  >>> seen = set()
  >>> B=  [item for item in A if not (item in seen or seen.add(item))]
  >>> B
  [1, 2, 3, 4]
  >>>

Michael




More information about the Python-list mailing list