top of page
Writer's pictureTaylor Etheredge

Working with nested dictionaries in Python

Updated: Nov 10, 2023

There are times when we need to work with nested dictionaries and I just happened to come across one when gathering data from an internal API at work. The data structure return was a nested dictionary that's right not a list of dictionaries, but instead a nested dictionary. Since this is in internal API at work, I can't actually give you the real data now can I : ). So here is a made up nested dictionary:


{
    "Food": {
        "Pork Shoulder": 1,
        "Beef Brisket": 2,
        "Chicken Breast": 3,
    },
    "More Food": {
        "Pork Shoulder": 2,
        "Beef Brisket": 4,
        "Chicken Breast: 1,
        "Meatloaf": 3,
    },
}

So here is the predicament at hand, the keys of the nested dictionaries are capitalized and have spaces in them. More than likely we don't work with capitalized names that also have spaces in them in our data structures, because when saving our data to a database the columns in the tables are not capitalized with spaces in them either. So we need to find a way to transpose those keys to the correct name in order to save the information to the database.


First off, what we are going to do is create a dictionary in a constant that is going to represent the name of the keys coming from the API to the name of the tables respective column. The mapping will look something like this:


FOOD = {
    "Pork Shoulder": "pork_shoulder",
    "Beef Brisket": "beef_brisket",
    "Chicken Breast": "chicken_breast",
    "Meatloaf": "meatloaf",
}

Then we can create a whole new empty dictionary and assign the parent keys from the original dictionary that is returned from the API, as the keys in the empty dictionary. The values will still be the original data assigned to the parent keys. However, the nested dictionary keys will now be changed to have the correct name to match the name of the tables column respectively. So let's create a new method for taking in the original dictionary that is returned from the API and then process the dictionary into the empty dictionary like so:


data = {
    "Food": {
        "Pork Shoulder": 1,
        "Beef Brisket": 2,
        "Chicken Breast": 3,
    },
    "More Food": {
        "Pork Shoulder": 2,
        "Beef Brisket": 4,
        "Chicken Breast: 1,
        "Meatloaf": 3,
    },
}
    
def process_food_types(data):
    all_food = {}
    for k, v in data.items():
        all_food[k] = {FOOD[key]: t for key, t in v.items()}

    return all_food

So line by line, we create a new empty dictionary called all_food to rebuild the names of the keys in the nested dictionaries. Then we loop over the key value pairs in the parent dictionaries. The next line is where the assignment happens to the empty dictionary called all_food. This line is a list comprehension statement that shortens the code and assigns the rest of the data to the all_food dictionary. The parent key in the parent dictionary is assigned as the parent key in the all_food dictionary. Then the parent dictionary is assigned to the new key pair of the FOOD constant. That key lookup returns the value in the FOOD dictionary constant. Then the next part of the list comprehension loops over all the key value pairs in the nested dictionaries. It uses the key in the nested dictionary as the look up in the FOOD constant dictionary. This returns the value of the FOOD constant dictionary for that particular key. Then the values of the nested dictionary are assigned to the new nested keys name. So when all said and done you will have output that looks like this:



{
    "Food": {
        "pork_shoulder": 1,
        "beef_brisket": 2,
        "chicken_breast": 3,
    },
    "More Food": {
        "pork_shoulder": 2,
        "beef_brisket": 4,
        "chicken_breast: 1,
        "meatloaf": 3,
    },
}


I hope this helps explain how to deal with nested dictionaries and changing the names of the keys in the nested dictionaries to make life easier when saving the data to the database. Thank you for reading and stay tuned in for more content.

13 views0 comments

Comments


bottom of page