How to remove these duplicates in a list (python)

前端 未结 5 2020
野的像风
野的像风 2021-01-03 15:26
biglist = 

[ 

    {\'title\':\'U2 Band\',\'link\':\'u2.com\'}, 
    {\'title\':\'ABC Station\',\'link\':\'abc.com\'}, 
    {\'title\':\'Live Concert by U2\',\'link         


        
5条回答
  •  时光说笑
    2021-01-03 15:50

    Make a new dictionary, with 'u2.com' and 'abc.com' as the keys, and your list elements as the values. The dictionary will enforce uniqueness. Something like this:

    uniquelist = dict((element['link'], element) for element in reversed(biglist))
    

    (The reversed is there so that the first elements in the list will be the ones that remain in the dictionary. If you take that out, then you will get the last element instead).

    Then you can get elements back into a list like this:

    biglist = uniquelist.values()
    

提交回复
热议问题