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
Dwith the right keys but arbitrary order,{key: D[key] for key in vehicle_type}would produce a new dict in the right order. Change to{key: D.get(key) for ...if some of the dicts might have missing keys (which this code would turn into explicitNones).