You need to sort the other lists based on the order of 'Functions'
, and sort that list last. One way to implement this is using zip
to combine e.g. 'Functions'
and 'Action'
into a single list [('Transfer Amount', 'N'), ...]
, sort that, then extract the second value from each pair (using e.g. map
and operator.itemgetter
):
from operator import itemgetter
data = {'Role Name': ['Administrator'],
'Approval': ['N', 'N', 'N', 'N', 'N'],
'Functions': ['Transfer Amount', 'Withdraw Amount', 'Admin Action', 'Create Users', 'User Deletion'],
'Approve': ['N', 'Y', 'N', 'N', 'N'],
'Action': ['N', 'Y', 'Y', 'Y', 'Y']}
for key in ['Action', 'Approval', 'Approve']:
data[key] = map(itemgetter(1), sorted(zip(data['Functions'], data[key])))
data['Functions'] = sorted(data['Functions'])
This gives me the answer you’re looking for:
{'Role Name': ['Administrator'],
'Approval': ['N', 'N', 'N', 'N', 'N'],
'Functions': ['Admin Action', 'Create Users', 'Transfer Amount', 'User Deletion', 'Withdraw Amount'],
'Approve': ['N', 'N', 'N', 'N', 'Y'],
'Action': ['Y', 'Y', 'N', 'Y', 'Y']}
1
solved Python: Sorting list in dictionary by key name