1
I have a list of items and a list of submenus (imagine a menu), because then, I would like to create a dictionary separating the items by the key of your menu. I couldn’t develop a logic that worked for this problem. Follow code and an example draft of a desired output. thank you.
items = [('a', 1),('b', 1),('c', 2)]
submenu = [('x', 1), ('y', 2)]
dictry = []
for i in range(len(items)):
d = {}
d['id_item'] = items[0][0]
d['id_submenu'] = items[0][1]
dictry.append(d)
items.pop(0)
print(dictry)
#SaidaEsperada#
{
"Menu": [
{
"X": [
{
"id_item": "a",
"id_submenu": 1
},
{
"id_item": "b",
"id_submenu": 1
}
]
},
{
"Y": [
{
"id_item": "c",
"id_submenu": 2
}
]
}
]
}
Rodolfo, this expected output that you put in the question is not valid, try to create a valid dictionary, because with this structure you can not understand how the output really should be.
– Andre
@user140828 I edited, but basically I need to consolidate the items based on their corresponding submenu. see if you understand now please. thank you
– Rodolfo Sousa