Python: Ordering Nested Dictionaries

  dictionary, nested, python, sorting

I want to order some nested dictionaries to match the order of this list:

vehicle_type = ['Car', 'Bike', 'Bus']

This is currently what the nested dictionary looks like:

vehicles = {'2018': {'Bike': 2542, 'Bus': 37, 'Car': 33432}, '2012': {'Car': 68122, 'Bike': 164, 'Bus': 1653}, '2001': {'Car': 12127, 'Bus': 324, 'Bike': 4222}}

I have organised the outer keys which represent the years with:

ordered_dict = [(k, vehicles[k]) for k in sorted(list(vehicles.keys()))]

I’m just unsure on how I go through each year and organise their keys to the order of my vehicle_type list?
I’m probably being dumb

Source: Python Questions

LEAVE A COMMENT