Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicates in a list while keeping its order (Python)

Tags:

This is actually an extension of this question. The answers of that question did not keep the "order" of the list after removing duplicates. How to remove these duplicates in a list (python)

biglist =   [       {'title':'U2 Band','link':'u2.com'},      {'title':'Live Concert by U2','link':'u2.com'},     {'title':'ABC Station','link':'abc.com'}  ] 

In this case, the 2nd element should be removed because a previous "u2.com" element already exists. However, the order should be kept.

like image 744
TIMEX Avatar asked Oct 11 '09 00:10

TIMEX


1 Answers

use set(), then re-sort using the index of the original list.

>>> mylist = ['c','a','a','b','a','b','c'] >>> sorted(set(mylist), key=lambda x: mylist.index(x)) ['c', 'a', 'b'] 
like image 184
egafni Avatar answered Feb 20 '23 07:02

egafni