I need to call a recursive function to print the contents of the nested dictionary like this on separate lines:
lunch : {"sandwich": 2, "chips": 1, "drink": 1}
dinner : {"ribs": 6, "mac_cheese": 1, "roll": 1, "pie": 1}
I tried the following code but it is just iterating over the keys of the nested dictionary
# recursive function for printing out meal plans
def print_meals(meal_plan):
for key, value in meal_plan.items():
if type(value) is dict:
print_meals(value)
else:
print(key, ":", value)
# create an empty dictionary
meal_plan = {}
# create individual dictionaries for each meal
lunch = {"sandwich": 2, "chips": 1, "drink": 1}
dinner = {"ribs": 6, "mac_cheese": 1, "roll": 1, "pie": 1}
#create a nested dictionary
meal_plan["lunch"] = lunch
meal_plan["dinner"] = dinner
# call recursive function 'print_meals'
print_meals(meal_plan)
CodePudding user response:
you can define your function
like below:
>>> def print_meals(dct):
... for k,v in dct.items():
... print(f'{k} : {v}');print()
Output:
>>> meal_plan
{'lunch': {'sandwich': 2, 'chips': 1, 'drink': 1},
'dinner': {'ribs': 6, 'mac_cheese': 1, 'roll': 1, 'pie': 1}}
>>> print_meals(meal_plan)
lunch : {'sandwich': 2, 'chips': 1, 'drink': 1}
dinner : {'ribs': 6, 'mac_cheese': 1, 'roll': 1, 'pie': 1}
CodePudding user response:
Here is an idea:
def print_meals(meal_plan, depth=0):
if isinstance(meal_plan, dict):
for k, v in meal_plan.items():
print(' '*depth, k, ':')
print_meals(v, depth 1)
else:
print(' '*depth, meal_plan)
print_meals('sandwich')
# sandwich
print_meals({'sandwich': 1})
# sandwich :
# 1
print_meals({'lunch': {'sandwich': 2, 'chips': 1, 'drink': 1}, 'dinner': {'ribs': 6, 'mac_cheese': 1, 'roll': 1, 'pie': 1}})
# lunch :
# sandwich :
# 2
# chips :
# 1
# drink :
# 1
# dinner :
# ribs :
# 6
# mac_cheese :
# 1
# roll :
# 1
# pie :
# 1
print_meals({'monday': {'lunch': {'sandwich': 1, 'apple': 2}, 'dinner': {'soup': 1, 'yaourt': 1}}, 'tuesday': {'breakfast': 'pain au chocolat', 'lunch': {'salad': 2, 'lasagna': 1}, 'dinner': {'beans':3}}})
# monday :
# lunch :
# sandwich :
# 1
# apple :
# 2
# dinner :
# soup :
# 1
# yaourt :
# 1
# tuesday :
# breakfast :
# pain au chocolat
# lunch :
# salad :
# 2
# lasagna :
# 1
# dinner :
# beans :
# 3
Alternatively, for fewer line breaks, but assumes the meal plan is a dict:
def print_meals(meal_plan, depth=0):
for k,v in meal_plan.items():
if isinstance(v, dict):
print(' '*depth, k, ':')
print_meals(v, depth 1)
else:
print(' '*depth, k, ':', v)
print_meals({'sandwich': 1})
# sandwich : 1
print_meals({'lunch': {'sandwich': 2, 'chips': 1, 'drink': 1}, 'dinner': {'ribs': 6, 'mac_cheese': 1, 'roll': 1, 'pie': 1}})
# lunch :
# sandwich : 2
# chips : 1
# drink : 1
# dinner :
# ribs : 6
# mac_cheese : 1
# roll : 1
# pie : 1
print_meals({'monday': {'lunch': {'sandwich': 1, 'apple': 2}, 'dinner': {'soup': 1, 'yaourt': 1}}, 'tuesday': {'breakfast': 'pain au chocolat', 'lunch': {'salad': 2, 'lasagna': 1}, 'dinner': {'beans':3}}})
# monday :
# lunch :
# sandwich : 1
# apple : 2
# dinner :
# soup : 1
# yaourt : 1
# tuesday :
# breakfast : pain au chocolat
# lunch :
# salad : 2
# lasagna : 1
# dinner :
# beans : 3