When I tried to run this code I'm getting a TypeError
like this:
items[i[id]] = i
TypeError: string indices must be integers
And here is my code:
x = {
'1': {
'id': 1,
'name': 'Burger',
'price': 10,
'quantity': 2
},
'2':{
'id': 2,
'name': 'Pizza',
'price': 15,
'quantity': 5
},
'3': {
'id': 1,
'name': 'Burger',
'price': 10,
'quantity': 5
},
}
items = {}
counter = 0
for i in x:
if items:
for j in items:
if j['id'] == i['id']:
j['quantity'] = j['quantity'] + i['quantity']
counter = 1
else:
items[i[id]] = i
if counter != 1:
items[i[id]] = i
print(items)
