pythonloopsdictionarykeyitems

Print available and show missing items from two dictionaries


I have a dictionary containing my available food in my fridge and I want to print the recipes I can make but also the ones that I could make if I had enough items (showing the number of items missing) and also print the ones that have an ingredient missing and thus what ingredient it is. I just managed to show them all.

fridge = {
    "orange" : 5,
    "citron" : 3,
    "sel" : 100,
    "sucre" : 50,
    "farine" : 250,
    "tomates" : 6,
    "huile" : 100,
    "pomme" : 1,
    "lait" : 1,
    "pois chiche" : 1,
}
    
recipes = {
    "jus_de_fruit" : {
        "orange" : 3,
        "citron" : 1,
        "pomme" : 2
    },
    "salad" : {
        "tomates" : 4,
        "huile" : 10,
        "sel" : 3
    },
    "crepes" : {
        "lait" : 1,
        "farine" : 250,
        "oeufs" : 2
        
    },
    "glace" : {
        "bac a glace" : 1,
        "coulis abricot" : 1,
        "batonnet" : 1
        }
    }

recette_dispo=[]
counter = 0
for recipe, recipe_contents in recipes.items():
     if all(elem in list(fridge.keys()) for elem in list(recipes[recipe].keys())):
        if all(recipe_contents[elem] <= fridge[elem] for elem in recipe_contents):
            print(f'\n \n ****   nice! you can make this recipe : {recipe}  ****')
            counter += 1 
            recette_dispo=recipe
        else : 
            print(f'You have all ingredients but not enough in quantity for: {recipe}, you need: {list(recipe_contents.items())}')           
  
     else :         
            print(f'\n\n Tu nas pas tous les ingrédients pour : {recipe}')
            print(f' You need these ingredients in order to make it  : {list(recipe_contents.items())}')
            

            
            
print("\n I can make in total", counter, "recipe(s) from the ingredients in my fridge:",recette_dispo)

Output:

You have all ingredients but not enough in quantity for: jus_de_fruit, you need:[('orange', 3), ('citron', 1), ('pomme', 2)]
#here I would like instead only the missing numbers of ingredients

     ****  nice! you can make this recipe : salad  ****
    
    
    You don't have all ingredients for: crepes
    You need these ingredients : [('lait', 1), ('farine', 250), ('oeufs', 2)]
#here I would like instead not all the ingredients of the recipe but only the ones missing in my fridge
    
    
     Tu nas pas tous les ingrédients pour : glace
     You need these ingredients: [('bac a glace', 1), ('coulis abricot', 1), ('batonnet', 1)]
    
     I can make in total 1recipe(s) from the ingredients in my fridge: salade

Solution

  • You can use this example how to find missing items from the fridge:

    fridge = {
        "orange": 5,
        "citron": 3,
        "sel": 100,
        "sucre": 50,
        "farine": 250,
        "tomates": 6,
        "huile": 100,
        "pomme": 1,
        "lait": 1,
        "pois chiche": 1,
    }
    
    recipes = {
        "jus_de_fruit": {"orange": 3, "citron": 1, "pomme": 2},
        "salad": {"tomates": 4, "huile": 10, "sel": 3},
        "crepes": {"lait": 1, "farine": 250, "oeufs": 2},
        "glace": {"bac a glace": 1, "coulis abricot": 1, "batonnet": 1},
    }
    
    for recipe_name, ingredients in recipes.items():
        print(f"Recipe: {recipe_name}")
        if (missing := ingredients.keys() - fridge.keys()) :
            print("Missing ingredients:", missing)
        else:
            common = {
                k: fridge[k] - ingredients[k]
                for k in ingredients.keys() & fridge.keys()
            }
    
            if all(v >= 0 for v in common.values()):
                print("All ingredients are OK")
            else:
                for i, v in common.items():
                    if v < 0:
                        print(f"Ingredient {i}: missing {-v} items")
        print("-" * 80)
    

    Prints:

    Recipe: jus_de_fruit
    Ingredient pomme: missing 1 items
    --------------------------------------------------------------------------------
    Recipe: salad
    All ingredients are OK
    --------------------------------------------------------------------------------
    Recipe: crepes
    Missing ingredients: {'oeufs'}
    --------------------------------------------------------------------------------
    Recipe: glace
    Missing ingredients: {'batonnet', 'coulis abricot', 'bac a glace'}
    --------------------------------------------------------------------------------