I may have been massively over complicating things here, this appears to do what I want (ignoring the insertion of completely new markets / selection_id’s for now):
new_order_list = []
for x in updated_orders:
for y in x['Live_orders']:
orders_to_update = {}
orders_to_update.update({'Market_id': x['Market_id'],
'Selection_id': y['selection_id'],
'live_orders': y['live_orders']})
new_order_list.append(orders_to_update)
for z in new_order_list:
for a in original_state:
for b in a['position_list']:
if z['Market_id'] == b['Market_id'] and z['Selection_id'] == b['Selection_id']:
#for c in b['Orders']:
print(b['Market_id'], b['Selection_id'], z['live_orders'])
b.update({'Orders': z['live_orders']})
I must have missed the obvious solution and was looking for something far too complex, but hopefully this will help someone else as this must be a fairly common problem (yet with very few posts on the solution)!
Here is how I tackled the updates where the orders were not in the original_state
:
uo_list = []
for i in updated_orders:
uo_list.append(i['Market_id'])
os_list = []
for j in original_state:
os_list.append(j['Market_id'])
new_list = list(set(uo_list).difference(os_list))
for i in updated_orders:
for j in new_list:
if i['Market_id'] == j:
print('Append this as required', i)
solved How to compare a nested list of Dicts to another nested list of Dicts and update the values in one from the other