how to remove duplicated elements in a list?

Kevin Yuan farproc at gmail.com
Mon Dec 19 04:17:58 EST 2005


2005/12/19, Brian van den Broek <broek at cc.umanitoba.ca>:
>
> bonono at gmail.com said unto the world upon 2005-12-19 02:27:
> > Steve Holden wrote:
> >
> >>Kevin Yuan wrote:
> >>
> >>>How to remove duplicated elements in a list? eg.
> >>>[1,2,3,1,2,3,1,2,1,2,1,3] -> [1,2,3]?
> >>>Thanks!!
> >>>
> >>
> >>  >>> list(set([1,2,3,1,2,3,1,2,1,2,1,3]))
> >>[1, 2, 3]
> >>
> >
> > Would this have the chance of changing the order ? Don't know if he
> > wants to maintain the order or don't care though.
>
> For that worry:
>
> >>> orig_list = [3,1,2,3,1,2,3,1,2,1,2,1,3]
> >>> new_list = list(set(orig_list))
> >>> new_list.sort(cmp= lambda x,y: cmp(orig_list.index(x),
> orig_list.index(y)))
> >>> new_list
> [3, 1, 2]


Thanks a lot!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20051219/c6d23f45/attachment.html>


More information about the Python-list mailing list