with open('metrics_sorted_by_weight.json', 'w') as f: metrics_json = json.dumps(metrics_abs_sorted[::-1]) json.dump(metrics_json, f)