Home > Mobile >  List of Dictionaries Python: Append a new key, value pair to dictionary if existing key, value pair
List of Dictionaries Python: Append a new key, value pair to dictionary if existing key, value pair

Time:11-13

original_list = [{"record1": 1, "record2": "loremipsum1", "record3": "loremipsum2", "record4": "loremipsum3"}, 
{"record1": 2, "record2": "loremipsum4", "record3": "loremipsum5", "record4": "loremipsum6"}, 
{"record1": 3, "record2": "loremipsum7", "record3": "loremipsum8", "record4": "loremipsum9"}, 
{"record1": 4, "record2": "loremipsum10", "record3": "loremipsum12", "record4": "loremipsum13"}, 
{"record1": 5, "record2": "loremipsum11", "record3": "loremipsum12", "record4": "loremipsum13"}]
to_append = [{"record1": 1, "new_record": "new1"},
{"record1": 2, "new_record": "new2"}]
output = [{"record1": 1, "record2": "loremipsum1", "record3": "loremipsum2", "record4": "loremipsum3", "new_record": "new1"}, 
{"record1": 2, "record2": "loremipsum4", "record3": "loremipsum5", "record4": "loremipsum6", "new_record": "new2"}, 
{"record1": 3, "record2": "loremipsum7", "record3": "loremipsum8", "record4": "loremipsum9"}, 
{"record1": 4, "record2": "loremipsum10", "record3": "loremipsum12", "record4": "loremipsum13"}, 
{"record1": 5, "record2": "loremipsum11", "record3": "loremipsum12", "record4": "loremipsum13"}]

The output is to append the new_record to the dictionary in the list if value of record1 field is the same and do nothing if new_record does not exists. Better if itertools is used.

I tried a few solutions but it returns an array with appended dictionary only if new_record exists - ignores all other dictionaries.

Could anyone help with it please? TIA

CodePudding user response:

You can check every dictionary in original_array against every dictionary in array_to_append; if you find a match between record1 in any specific pair, then update the original one:

for d1 in original_array:
    for d2 in array_to_append:
        if d1['record1'] == d2['record1']:
            d1.update(d2)

Resulting in:

>>> original_array 
[{'record1': 1,
  'record2': 'loremipsum1',
  'record3': 'loremipsum2',
  'record4': 'loremipsum3',
  'new_record': 'new1'},
 {'record1': 2,
  'record2': 'loremipsum4',
  'record3': 'loremipsum5',
  'record4': 'loremipsum6',
  'new_record': 'new2'},
 {'record1': 3,
  'record2': 'loremipsum7',
  'record3': 'loremipsum8',
  'record4': 'loremipsum9'},
 {'record1': 4,
  'record2': 'loremipsum10',
  'record3': 'loremipsum12',
  'record4': 'loremipsum13'},
 {'record1': 5,
  'record2': 'loremipsum11',
  'record3': 'loremipsum12',
  'record4': 'loremipsum13'}]

A few things to keep in mind:

  • This is modifying original_array and the dictionaries inside, not creating new objects.
  • This raises a KeyError if any dictionary does not have a 'record1' key. If you expect some dictionaries to not have that field, you will have to add handling that.
  • If there is more than one entry in array_to_append with a matching 'record1' for a given dictionary, you could have issues with the "new_records" overwriting one another.
  • Related