Home > Blockchain >  Is there a better/more pythonic way to update dictionaries?
Is there a better/more pythonic way to update dictionaries?

Time:02-11

This is a class assignment. I am trying to update the values of multiple dictionary keys simultaneously.

The code is supposed to subtract the 'ingredients' used by the various drinks from the resources dictionary.

I have it working on a drink by drink basis but I feel certain there is more pythonic way to do this. This code only updates for 'cappuccino'

I looked into using the update() method but but not sure how/if it applies.python

MENU = {
    "espresso": {
        "ingredients": {
            "water": 50,
            "coffee": 18,
        },
        "cost": 1.5,
    },
    "latte": {
        "ingredients": {
            "water": 200,
            "milk": 150,
            "coffee": 24,
        },
        "cost": 2.5,
    },
    "cappuccino": {
        "ingredients": {
            "water": 250,
            "milk": 100,
            "coffee": 24,
        },
        "cost": 3.0,
    }
}

resources = {
    "water": 300,
    "milk": 200,
    "coffee": 100,
}


#the bracket notation is how I access the values of the different keys in the dictionary. 
#For the dict MENU we need to access the deeper levels of the dictionary by using the multiple 
#brackets to get to the value of the key. I then subtract the amounts used by the drinks from the resources.`
#this approach works, but I feel like there is a more pythonic approach.

cappuccino_remaining_water = resources['water'] - MENU['cappuccino']['ingredients']['water']
cappuccino_remaining_milk = resources['milk'] - MENU['cappuccino']['ingredients']['milk']
cappuccino_remaining_coffee = resources['coffee'] - MENU['cappuccino']['ingredients']['coffee']

resources['water'] = cappuccino_remaining_water
resources['milk'] = cappuccino_remaining_milk
resources['coffee'] = cappuccino_remaining_coffee

print(resources)

CodePudding user response:

You can use a loop to iterate through all the ingredients required for your chosen drink and find the corresponding ingredient in you resources dictionary:

MENU = {
    "espresso": {
        "ingredients": {
            "water": 50,
            "coffee": 18,
        },
        "cost": 1.5,
    },
    "latte": {
        "ingredients": {
            "water": 200,
            "milk": 150,
            "coffee": 24,
        },
        "cost": 2.5,
    },
    "cappuccino": {
        "ingredients": {
            "water": 250,
            "milk": 100,
            "coffee": 24,
        },
        "cost": 3.0,
    }
}

resources = {
    "water": 300,
    "milk": 200,
    "coffee": 100,
}

drink = 'cappuccino'
for ingredient in MENU[drink]['ingredients']:
    if ingredient in resources:
        resources[ingredient] = resources[ingredient] - MENU[drink]['ingredients'][ingredient]

print(resources)

We're updating the resources dictionary by overwriting resources[ingredient] inside the loop, giving us:

{'water': 50, 'milk': 100, 'coffee': 76}
  • Related