Data append in a list using the previous data in Python
Question: İ 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?


Answer: Using the
[`defaultdict`](http://docs.python.org/2/library/collections.html#collections.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})
    

