Python - using tabular for a dictionary that's 3 levels deep

Viewed 23

Is it possible to use the tabulate function with a dictionary that is three levels deep? I took this dictionary from source and modified it a bit just to use as an idea.

MENU = {
    "espresso": 
        {
        "ingredients 1": 
            {
            "water": 50,
            "coffee": 18,
            },
        "ingredients 2": 
            {
            "water": 55,
            "coffee": 20,
            }
        }
    },
    "latte": 
        {
        "ingredients 1": 
            {
            "water": 200,
            "coffee": 24,
            },
        "ingredients 2": 
            {
            "water": 225,
            "coffee": 30,
            }
        }
    },
    "cappuccino": 
        {
        "ingredients 1": 
            {
            "water": 150,
            "coffee": 45,
            },
        "ingredients 2": 
            {
            "water": 155,
            "coffee": 40,
            }
        }
    }
}

What I'm looking for table-wise is something like this:

  Drink        Info        Water  Coffee
--------      ------       -----  ------
Espresso    Ingredients 1    50     18
Espresso    Ingredients 2    55     20
Latte       Ingredients 1    50     18
Latte       Ingredients 2    55     20
Cappuccino  Ingredients 1    50     18
Cappuccino  Ingredients 2    55     20

or this:

  Drink        Info        Water  Coffee
--------      ------       -----  ------
Espresso    Ingredients 1    50     18
            Ingredients 2    55     20
Latte       Ingredients 1    50     18
            Ingredients 2    55     20
Cappuccino  Ingredients 1    50     18
            Ingredients 2    55     20
1 Answers

Try:

MENU = {
    "espresso": {
        "ingredients 1": {
            "water": 50,
            "coffee": 18,
        },
        "ingredients 2": {
            "water": 55,
            "coffee": 20,
        },
    },
    "latte": {
        "ingredients 1": {
            "water": 200,
            "coffee": 24,
        },
        "ingredients 2": {
            "water": 225,
            "coffee": 30,
        },
    },
    "cappuccino": {
        "ingredients 1": {
            "water": 150,
            "coffee": 45,
        },
        "ingredients 2": {
            "water": 155,
            "coffee": 40,
        },
    },
}

fmt = "{:<20} {:<20} {:<20} {:<20}"

tmp = {}
for k1, v1 in MENU.items():
    for k2, v2 in v1.items():
        for k3, v3 in v2.items():
            tmp.setdefault((k1, k2), {})[k3] = v3

for (k1, k2), v in tmp.items():
    print(fmt.format(k1, k2, v["water"], v["coffee"]))

Prints:

Drink                Info                 Water                Coffee              
----------------------------------------------------------------------
espresso             ingredients 1        50                   18                  
espresso             ingredients 2        55                   20                  
latte                ingredients 1        200                  24                  
latte                ingredients 2        225                  30                  
cappuccino           ingredients 1        150                  45                  
cappuccino           ingredients 2        155                  40                            
Related