Skip to content
Advertisement

Updating values inside a python list

ItemList = [
    {'name': 'item', 'item_code': '473', 'price': 0},
    {'name': 'item', 'item_code': '510', 'price': 0},
    {'name': 'item', 'item_code': '384', 'price': 0},

]

data_1 = '510'
data_2 = 200

def update_item(data_1, data_2):
    for a in ItemList:
        if a['item_code'] == data_1:
            update_price = append(a['price'].data_2)
            return True

I want to update the price by using the function update_item. It fails at update_price = append(a[‘price’].data_2)

Advertisement

Answer

You can assign the value to the dictionary, with:

def update_item(data_1, data_2):
    for a in ItemList:
        if a['item_code'] == data_1:
            a['price'] = data_2
            return
Advertisement