Data append in a list using the previous data in Python

İ need to create a list in Python. It is a little complicated. The list will contain items which appends according to previous value. For example, suppose that my list contains

x : 11 (key, value pair)
y : 5
z : 6

if I want to add another "x" item with value "4", it will detect the previous value "11" of "x" and it will be recorded as " x : 15" as the sum with the previous one. I thought that I might use linked lists but I could not figure it out. Can you just provide me with other methods, data structures or code for this purpose?


L = [{'x':11}, {'y':2}, {'z':3}]

def addItem(L, key, value):
    for index, element in enumerate(L):
        if key in element:
            L.insert(index, {key, element[key] + value})        
    return L

L[:] = addItem(L, 'x', 4)

Using the defaultdict and calling a function:

from collections import defaultdict

def add_item(d, key, value):
    d[key] += value

d = defaultdict(int)
add_item(d, 'x', 11)
add_item(d, 'x', 4)
print d
defaultdict(<type 'int'>, {'x': 15})

Need Your Help

Change activity animation when clicking on listview item

android listview onitemclicklistener

Hi i have a listview with multiple items on them, when you click on an item it opens a new activity. I want to change that activities animation, right now the listview onitemClickListener calls ano...

Is there a testing tool that will make sure a web form is behaving appropriately in response to valid and invalid data?

forms validation testing selenium automated-tests

I am looking for a black-box type program/script/Selenium add-on/etc. that will generate different kinds of data, input it into a web form, hit SUBMIT, and test whether the correct response occurre...