Python list simultaneous update
Question: This seems to be a gotcha for me, I couldnt figure this out

    
    
    >>> from collections import Counter
    >>> tree = [Counter()]*3
    >>> tree
    [Counter(), Counter(), Counter()]
    >>> tree[0][1]+=1
    >>> tree
    [Counter({1: 1}), Counter({1: 1}), Counter({1: 1})]
    

Why does updating one Counter updates everything?


Answer: Using `[x] * 3`, the list references same item(`x`) three times.

    
    
    >>> from collections import Counter
    >>> tree = [Counter()] * 3
    >>> tree[0] is tree[1]
    True
    >>> tree[0] is tree[2]
    True
    >>> another_counter = Counter()
    >>> tree[0] is another_counter
    False
    
    >>> for counter in tree: print id(counter)
    ...
    40383192
    40383192
    40383192
    

Use list comprehension as Waleed Khan commented.

    
    
    >>> tree = [Counter() for _ in range(3)]
    >>> tree[0] is tree[1]
    False
    >>> tree[0] is tree[2]
    False
    
    >>> for counter in tree: print id(counter)
    ...
    40383800
    40384104
    40384408
    

