I have two lists of dictionaries. Both lists always have the same ids. I want to get the following result:
{9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}
The following code is working but I wonder if this is the best solution?
l1 = [{'id': 9, 'av': 4}, {'id': 10, 'av': 0}, {'id': 8, 'av': 0}]
l2 = [{'id': 9, 'nv': 45}, {'id': 10, 'nv': 0}, {'id': 8, 'nv': 30}]
l3 = {x['id']: {'av': x['av']} for x in l1}
l4 = {x['id']: {'nv': x['nv']} for x in l2}
{key: value.update(l4[key]) for key, value in l3.items()}
>> {9: {'av': 4, 'nv': 45}, 10: {'av': 0, 'nv': 0}, 8: {'av': 0, 'nv': 30}}
l1
andl2
have theirid
in the same order, is it always the case? \$\endgroup\$ – 301_Moved_Permanently Dec 7 '18 at 10:51