Please I need help with a problem that needs to be solved without pandas or numpy. I have two list of dictionaries i.e list1 and list2. I need to sort list2 by "post_code", and group th e sorted list2 by "code" before joining list1 and list2 by two different keys that have the same values. In list1 the key "practice" is equivalent to the key "code" in sorted list2. I need to join list1 and list2 by the equivalent keys of "practice" and "code".
list1=
[{'bnf_code': '0101010G0AAABAB',
'items': 2,
'practice': 'N81013',
'bnf_name': 'Co-Magaldrox_Susp 195mg/220mg/5ml S/F',
'nic': 5.98,
'act_cost': 5.56,
'quantity': 1000},
{'bnf_code': '0101021B0AAAHAH',
'items': 1,
'practice': 'A81001',
'bnf_name': 'Alginate_Raft-Forming Oral Susp S/F',
'nic': 1.95,
'act_cost': 1.82,
'quantity': 500},
{'bnf_code': '0101021B0AAALAL',
'items': 12,
'practice': 'A81002',
'bnf_name': 'Sod Algin/Pot Bicarb_Susp S/F',
'nic': 64.51,
'act_cost': 59.95,
'quantity': 6300},
{'bnf_code': '0101021B0AAAPAP',
'items': 3,
'practice': 'A81004',
'bnf_name': 'Sod Alginate/Pot Bicarb_Tab Chble 500mg',
'nic': 9.21,
'act_cost': 8.55,
'quantity': 180},
{'bnf_code': '0101021B0BEADAJ',
'items': 6,
'practice': 'A81003',
'bnf_name': 'Gaviscon Infant_Sach 2g (Dual Pack) S/F',
'nic': 28.92,
'act_cost': 26.84,
'quantity': 90}]
list2=
[{'code': 'A81001',
'name': 'THE DENSHAM SURGERY',
'addr_1': 'THE HEALTH CENTRE',
'addr_2': 'LAWSON STREET',
'borough': 'STOCKTON ON TEES',
'village': 'CLEVELAND',
'post_code': 'TS18 1HU'},
{'code': 'A81002',
'name': 'QUEENS PARK MEDICAL CENTRE',
'addr_1': 'QUEENS PARK MEDICAL CTR',
'addr_2': 'FARRER STREET',
'borough': 'STOCKTON ON TEES',
'village': 'CLEVELAND',
'post_code': 'TS18 2AW'},
{'code': 'A81003',
'name': 'VICTORIA MEDICAL PRACTICE',
'addr_1': 'THE HEALTH CENTRE',
'addr_2': 'VICTORIA ROAD',
'borough': 'HARTLEPOOL',
'village': 'CLEVELAND',
'post_code': 'TS26 8DB'},
{'code': 'A81004',
'name': 'WOODLANDS ROAD SURGERY',
'addr_1': '6 WOODLANDS ROAD',
'addr_2': None,
'borough': 'MIDDLESBROUGH',
'village': 'CLEVELAND',
'post_code': 'TS1 3BE'},
{'code': 'N81013',
'name': 'SPRINGWOOD SURGERY',
'addr_1': 'SPRINGWOOD SURGERY',
'addr_2': 'RECTORY LANE',
'borough': 'GUISBOROUGH',
'village': None,
'post_code': 'TS14 7DJ'}]
I have been able to sort list2 by post_code and group by code but I am lost with regards to how to join list1 and list2. Here is the code I used so far for the sorting and grouping.
import itertools
from operator import itemgetter
sorted_post_code = sorted(list2, key=itemgetter('post_code'))
for key, group in itertools.groupby(sorted_post_code, key=lambda x:x['code']):
#print (key),
print (list(group))
The expected out put is
joined_list=
list1=
[{'bnf_code': '0101010G0AAABAB',
'items': 2,
'practice': 'N81013',
'bnf_name': 'Co-Magaldrox_Susp 195mg/220mg/5ml S/F',
'nic': 5.98,
'act_cost': 5.56,
'quantity': 1000,
'code': 'N81013',
'name': 'SPRINGWOOD SURGERY',
'addr_1': 'SPRINGWOOD SURGERY',
'addr_2': 'RECTORY LANE',
'borough': 'GUISBOROUGH',
'village': None,
'post_code': 'TS14 7DJ'},
{'bnf_code': '0101021B0AAAHAH',
'items': 1,
'practice': 'A81001',
'bnf_name': 'Alginate_Raft-Forming Oral Susp S/F',
'nic': 1.95,
'act_cost': 1.82,
'quantity': 500,
'code': 'A81001',
'name': 'THE DENSHAM SURGERY',
'addr_1': 'THE HEALTH CENTRE',
'addr_2': 'LAWSON STREET',
'borough': 'STOCKTON ON TEES',
'village': 'CLEVELAND',
'post_code': 'TS18 1HU'},
{'bnf_code': '0101021B0AAALAL',
'items': 12,
'practice': 'A81002',
'bnf_name': 'Sod Algin/Pot Bicarb_Susp S/F',
'nic': 64.51,
'act_cost': 59.95,
'quantity': 6300,
'code': 'A81002',
'name': 'QUEENS PARK MEDICAL CENTRE',
'addr_1': 'QUEENS PARK MEDICAL CTR',
'addr_2': 'FARRER STREET',
'borough': 'STOCKTON ON TEES',
'village': 'CLEVELAND',
'post_code': 'TS18 2AW'},
{'bnf_code': '0101021B0AAAPAP',
'items': 3,
'practice': 'A81004',
'bnf_name': 'Sod Alginate/Pot Bicarb_Tab Chble 500mg',
'nic': 9.21,
'act_cost': 8.55,
'quantity': 180,
'code': 'A81004',
'name': 'WOODLANDS ROAD SURGERY',
'addr_1': '6 WOODLANDS ROAD',
'addr_2': None,
'borough': 'MIDDLESBROUGH',
'village': 'CLEVELAND',
'post_code': 'TS1 3BE'},
{'bnf_code': '0101021B0BEADAJ',
'items': 6,
'practice': 'A81003',
'bnf_name': 'Gaviscon Infant_Sach 2g (Dual Pack) S/F',
'nic': 28.92,
'act_cost': 26.84,
'quantity': 90,
'code': 'A81003',
'name': 'VICTORIA MEDICAL PRACTICE',
'addr_1': 'THE HEALTH CENTRE',
'addr_2': 'VICTORIA ROAD',
'borough': 'HARTLEPOOL',
'village': 'CLEVELAND',
'post_code': 'TS26 8DB'}]