i would like to add this:
{
"Category": "Fruit"
}
to this:
{
"Category": "Vegetable"
{
so I can have:
"Name": "Menu1",
"Categories":[
{
"Category": "Fruit"
{
}
"Category": "Vegetable"
{
]
I'm creating a list of dicts and sometimes I can have 2 dicts with the same key["Name"] and i would like to to avoid having the two dicts with the same name separated. Currently I'm doing this to check if the "Name" already exists and if not create it and add it to the list:
if len(List) != 0: #checking if the list has something
for x in List:
if menu['Name'] == x['Name']:
menu['Category'].append(Category) #if exist add data to the existing one
break
else:
List.append(menu) #if doesn't exist add it
break
else:
List.append(menu) #if is empty add an item
if List:is equivalent toif len(List) != 0:['Category']should bemenu['Category']breakin bothifandelse, you only process the first element ofList, so there's no reason to loop.else:should be unindented so it's on theforloop, notif.