0

compare multiple list of dicts and avoid duplicates Requirement need to insert data only if readid doesn't exist in payload

old_data= [
    {
      "abc": "205",
      "xyz": "11.38",
      "ReadId": "120"
    },
    {
     "abc": "201",
      "xyz": "11.38",
      "ReadId": "121"
    },
    {
      "abc": "109",
      "xyz": "11.38",
      "ReadId": "122"

    }
  ]

payload= [
      {
        "reading_id": 120,
        "abc": 205
      },
      {
        "reading_id": 121,
        "abc": 199
      },
      {
        "reading_id": 123,
        "abcl": 128
      }
    ]

here, is the code I tried but didn't find luck

for d in payload:
   for dt in old_data:
      if 'ReadId' in data:
          if data['ReadId'] != payload['reading_id']:     
                  insertGluco(d['reading_id'])

any help will be appreciated, thank you.

Harry
  • 31
  • 5
  • use ```set``` to find the difference. – sushanth May 22 '20 at 07:06
  • Does this answer your question? [Check if value already exists within list of dictionaries?](https://stackoverflow.com/questions/3897499/check-if-value-already-exists-within-list-of-dictionaries) – DavidDr90 May 22 '20 at 07:17
  • there it's checking in a single dict, here I need to check in multiple dicts @DavidDr90 – Harry May 22 '20 at 07:20
  • Is it an issue that you can't use that method on each dict, since they're already in a list? Similar to `for x in list_of_dicts: ` – Ryan Barnes May 22 '20 at 07:24

1 Answers1

0

Just modify your code slightly to use a set. It keeps all the read IDs of the old data and only inserts the payload dict if the read id is not present in the set.

A set stores unique values.

def insert(payload, old_data):
    read_ids = set(d.get("ReadId") for d in old_data)
    for igdata in payload:
        if igdata['reading_id'] not in read_ids:
            insertGluco(igdata['reading_id'], str(igdata['bgmmol']), str(igdata['bgmgdl']))
Abhishek J
  • 2,386
  • 2
  • 21
  • 22
  • ya this looks fine, but how do I check whether key ReadId exists or not becoz every object in old_data may not have key ReadId. – Harry May 22 '20 at 07:34
  • You can replace d["ReadId"] with d.get("ReadId") then. This will use a None as default if the key is not present. I've editted the answer. – Abhishek J May 22 '20 at 07:43