I am trying to traverse this kind of tree, the idea is to get all "titles". Notice that this structure can get bigger, it means that every title can get more subcategories in the future. any idea?
I am tryin to do this:
def continue_searching(item):
for i in len(item):
if categories[i]["subcategories"]:
continue_searching(i["subcategories"])
print(i["subcategories"])
def give_titles(categories):
for i in len(categories):
if categories[i]["subcategories"]:
continue_searching(i["subcategories"])
print(i['title'])
categories = [
{
"title": "Food",
"subcategories": [
{"title": "Bread"},
{
"title": "Meat",
"subcategories": [
{"title": "Pork",
"subcategories": [
{"title": "White Pork"},
{"title": "Red Pork"}
]
},
{"title": "Beef"},
],
},
{"title": "Cheese"},
],
},
{"title": "Drinks"},
]
give_titles(categories)
Expected output:
Food
-Bread
-Meat
--Pork
---White Pork
---Red Pork
--Beef
-Cheese
Drinks
Notice that i am not using recursion because it is not clear for me when stop the calls and i do not want to saturate the call stacks.