Comparing two dictionaries for specific value within list of dict

I have two list of dictionaries. I am looping through them and looking for matching id. If the id in the src_dict matches to destination I need to call an update method else an insert method. When I am using the below code I am getting unintended result.

This is the outcome I want. While updating i need to preserve rec_id from dest dict with corresponding values from src dict. insert is pretty much just the src dict elements thats not in dest dict. Appreciate any help!

records_update = [{'rec_id': 'abc', 'fields': {'id': 111, 'name': 'sam'}}, {'rec_id': 'xyz', 'fields': {'id': 333, 'name': 'name_changed_to_not_ross'}}]
#. the rec_id is from dest_dict while rest of the field should come from src_dict since these values could change that needs to be update
records_insert = [{"id": 444, "name": "jack"}]

src_dict = [{"id": 111, "name": "sam"}, {"id": 333, "name": "name_changed_to_not_ross"}, {"id": 444, "name": "jack"}]

dest_dict = [{"rec_id":"abc","fields":{"id":111,"name":"sam"}}, 
                {"rec_id":"pqr","fields":{"id":222,"name":"john"}},
                 {"rec_id":"xyz","fields":{"id":333,"name":"ross"}}]


records_update = []
records_insert = []
for rec_src in src_dict:
        for rec_dest in dest_dict:
            if rec_src['id'] == rec_dest['fields']['id']:
                print('match and add this element to update list')
                records_update.append(rec_src)
            else:
                print('no match add this element to insert list')
                records_insert.append(rec_src)```

You can create a dict indexed by IDs from dest_dict for efficient lookups, and then use list comprehensions to filter src_dict for respective records:

dest = {d['fields']['id']: d for d in dest_dict}
records_update = [dest[d['id']] for d in src_dict if d['id'] in dest]
records_insert = [d for d in src_dict if d['id'] not in dest]

Python Compare Two Lists of Dictionaries, That is, each dictionary contains the same items (keys and values) in both lists. The order of elements in each dictionary is irrelevant. Compare list� Python Dictionary are defined into two elements Keys and Values. Dictionaries do not store their information in any particular order, so you may not get your information back in the same order you entered it. Keys will be a single element; Values can be a list or list within a list, numbers, etc.

You need to do an append to insert list only when no append is done to update list.

records_update = []
records_insert = []
for rec_src in src_dict:
    flag = 0
    for rec_dest in dest_dict:
        if rec_src['id'] == rec_dest['fields']['id']:
            print('match and add this element to update list')
            records_update.append(rec_dest)
            flag = 1
    if flag != 1:
        print('no match add this element to insert list')
        records_insert.append(rec_src)

print(records_update)
# [{'rec_id': 'abc', 'fields': {'id': 111, 'name': 'sam'}}, {'rec_id': 'xyz', 'fields': {'id': 333, 'name': 'ross'}}]
print(records_insert)
# [{'id': 444, 'name': 'jack'}]

How to compare dictionaries in Python, Use == Comparing two dictionaries checks each corresponding key-value pair add the corresponding key: value pairs that exist in the second dictionary to a new Further reading: List comprehensions provide a concise way to create lists . Considering one can instantiate a dictionary using dict() it is probably advisable not to name variables “dict”. See for example this case: See for example this case: dict = "foo" bar = dict(one=1,two=2) print bar

I am able to achieve the desired result using this:

records_update = []
records_insert = []
for rec_src in src_dict:
    flag = 0
    for rec_dest in dest_dict:
        if rec_src['id'] == rec_dest['fields']['id']:
            new_dict = {}
            print('match and add this element to update list')
            new_dict['rec_id'] = rec_dest['rec_id']
            new_dict['fields'] = rec_src
            records_update.append(new_dict)
            flag = 1
    if flag != 1:
        print('no match add this element to insert list')
        records_insert.append(rec_src)

records_update
[{'rec_id': 'abc', 'fields': {'id': 111, 'name': 'sam'}},
 {'rec_id': 'xyz', 'fields': {'id': 333, 'name': 'name_changed_to_not_ross'}}]
records_insert
[{'id': 444, 'name': 'jack'}]

Just curious how I can achieve the result using @blhsing approach since that seems to be more efficient approach

ids = {d['fields']['id'] for d in dest_dict}
records_update = [dest[d['id']] for d in src_dict if d['id'] in dest]
records_insert = [d for d in src_dict if d['id'] not in dest]

How to compare two different dictionary values in Python, You compare dictionary values by accessing the dictionary with the appropria In Python, any difference between using [] and list() or between {} and dict()? a way to extract specific keys and values from an ordered dictionary in Python? Python dictionary method cmp() compares two dictionaries based on key and values. Return Value. This method returns 0 if both dictionaries are equal, -1 if dict1

Python script to compare the keys of 2 dictionaries and if equal print , This python code uses a dictionary comprehension to iterate through d1 's keys and, if the key is in both d1 and d2 , store the key in d3 with the value from d2 . This is helpful when you have difference between the dictionaries and you don't want to lose information. You can check how to merge two dictionaries here: Python How to Merge Dictionaries - examples for beginners. Now if you want to compare two dictionaries based on their values then you can use:

Finding the Intersection of Two Dictionaries, Given two dictionaries, you need to find the set of keys that are in both dictionaries. The keys method produces a list of all the keys of a dictionary. differences are almost substantial, they pale in comparison with the timing of the bad way,� Python dictionary method values() returns a list of all the values available in a given dictionary. Syntax. Following is the syntax for values() method − dict.values() Parameters. NA. Return Value. This method returns a list of all the values available in a given dictionary. Example. The following example shows the usage of values() method.

python comparing list values to keys in list of dicts, Python Compare Two Lists of Dictionaries, In this post, we look at how to Python, Given two dictionaries dic1 and dic2 which may contain same-keys, find the diff in two dictionary keys' values Including Nested Keys � Python | Merging two� Note that there are only two key:value pairs in output dictionary but the input list contains three tuples. The first element(i.e. key) is same for first and third tuple and two keys can never be same. Method #6: Using Json

Comments
  • Thanks is there a way to construct the update list like this records_update =:[{'rec_id': 'abc', 'fields': {'id': 111, 'name': 'sam'}, {'rec_id': 'xyz', 'fields': {'id': 333, 'name': 'ross'}}} that way I don't loose the rec_id from dest dict
  • Thanks is there a way to construct the update list like this:[{'rec_id': 'abc', 'fields': {'id': 111, 'name': 'sam'}, {'rec_id': 'xyz', 'fields': {'id': 333, 'name': 'ross'}}} that way I don't loose the rec_id from dest dict
  • @DaKuang, yes. Replace the append line with records_update.append(rec_dest).
  • Thanks I updated my question actually I need only the rec_id from the dest but rest of the fields in the fields key should come from. source since it could change.