Remove duplicates key from list of dictionaries python

后端 未结 4 598
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 14:26

I am trying to remove the duplicates from following list

 distinct_cur = [{\'rtc\': 0, \'vf\': 0, \'mtc\': 0, \'doc\': \'good job\', \'foc\': 195, \'st\': 0         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-05 15:09

    I see 2 similar solutions that depend on your domain problem: do you want to keep the first instance of a key or the last instance?

    Using the last (so as to overwrite the previous matches) is simpler:

    d = {r['doc']: r for r in distinct_cur}.values()
    

提交回复
热议问题