0

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

  
5
  • 1
    In Python empty lists are falsey you if List: is equivalent to if len(List) != 0: Commented Nov 5, 2020 at 19:55
  • ['Category'] should be menu['Category'] Commented Nov 5, 2020 at 19:57
  • 1
    If you break in both if and else, you only process the first element of List, so there's no reason to loop. Commented Nov 5, 2020 at 19:58
  • 1
    Maybe the first else: should be unindented so it's on the for loop, not if. Commented Nov 5, 2020 at 19:58
  • give examples of input and desired output Commented Nov 5, 2020 at 20:05

2 Answers 2

1

You should be appending to x['Category'], not an unnamed list that contains the string 'Category'.

The else: block that adds the name should be on the for loop. This will execute if the loop ends normally instead of from break, which means the name wasn't found.

There's no need to check the length of List first. If it's empty, the loop will end normally and the else: block will execute.

for x in List:
    if menu['Name'] == x['Name']:
        x['Category'].append(Category) #if exist add data to the existing one
        break
else:    
    List.append(menu) #if doesn't exist add it
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer! it helped me a lot resolving the problem but i had to change it a bit
0

So what I did was this:

for item in my_list:
    if item['Name'] == menu['Name']: #Exist in list
        print('Exist')
        var = []
        var.append({'Category': Category})
        x['Categories'] += var
        cod = []
        cod.append({'Code': code})
        x['Codes'] += cod
        break
else:
    my_list.append(menu) #doesn't exist
    print('not matched')

4 Comments

How does this add another category to the Categories list?
That's the same as x['Categories'].append({'Category': Category}). There's no need to create another list.
In my answer I assumed that Category was a dictionary like {'Category': "Fruit"}, not a string.
it wasn't working for me:( when i was using .append(something) that something was overwriting the previous value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.