0

I have a list of dictionaries and I would like to remove all dicts that are duplicates in number. Keep only the last!

Example:

List of dicts:

student_data = [{'number':'1234', 'url':'www.abc.com'}, {'number':'9999', 'url':'www.abc.com'}, {'number':'1234', 'url':'www.xyz.com'}] 

As you can see in we have 2 dicts where number is 1234. Therefore I remove the first dict and keep the last:

List of dicts:

student_data = [{'number':'9999', 'url':'www.abc.com'}, {'number':'1234', 'url':'www.xyz.com'}] 

Any idea how to do this?

PParker
  • 1,419
  • 2
  • 10
  • 25
  • 1
    This answers your question: https://stackoverflow.com/questions/11092511/python-list-of-unique-dictionaries – idar Oct 01 '20 at 14:32
  • The duplicate assumes that a dictionary is repeated if it has the same `id`. It does not check the other fields. Wrong dupe – yatu Oct 01 '20 at 14:37
  • This one is more like it https://stackoverflow.com/questions/6280978/how-to-uniqify-a-list-of-dict-in-python/6281063 . This should do `[dict(y) for y in set(tuple(x.items()) for x in L)]` – yatu Oct 01 '20 at 14:39

0 Answers0