duplicate items in a list

Daniel Schüle uval at rz.uni-karlsruhe.de
Mon Nov 21 12:51:01 CET 2005


Shi Mu wrote:
> I used the following method to remove duplicate items in a list and
> got confused by the error.
> 
> 
>>>>a
> 
> [[1, 2], [1, 2], [2, 3]]
> 
>>>>noDups=[ u for u in a if u not in locals()['_[1]'] ]
> 
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
> TypeError: iterable argument required

 >>> a
[[1, 2], [1, 2], [2, 3]]
 >>> c=[]
 >>> for x in a:
...     if x not in c: c.append(x)
...
 >>> c
[[1, 2], [2, 3]]

or (Python 2.4)


 >>> a
[[1, 2], [1, 2], [2, 3]]
 >>> set([frozenset(u) for u in a])
set([frozenset([1, 2]), frozenset([2, 3])])

hth Daniel




More information about the Python-list mailing list