On 17Nov2019 12:26, Iranna Mathapati <iranna.gan...@gmail.com> wrote:
How to remove duplicates dict from the list of dictionary based on one of
the duplicate elements in the dictionary,

l = [{"component":"software", "version":"1.2" },
    {"component":"hardware", "version":"2.2",},
    {"component":"driver",   "version":"3.2",},
    {"component":"firmware", "version":"4.2",},
    {"component":"software", "version":"1.9",},
    {"component":"hardware", "version":"2.7",}
   ]

need to remove the dict if having same components [in the above example
components "software" and "hardware" is duplicated twice

expected output:

   [{"component":"software", "version":"1.2" },
    {"component":"hardware", "version":"2.2",},
    {"component":"driver",   "version":"3.2",},
    {"component":"firmware", "version":"4.2",}
   ]

I would be inclined to keep a mapping (eg another dict) keyed on the value of "component". When you process these lists, put the component/version dicts into the appropriate entry in the new dict. If an entry is already there, act appropriately (eg keep the existing entry, keep the new entry, compare the entries in some way and keep the "better" one, etc). Then at the end, walk the new dict and make a fresh list.

Not providing example code because this feels a little homeworky, so we expect you to attempt the code first.

Cheers,
Cameron Simpson <c...@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to