Dictionary that counts occurrences of its items
Question: I'd like to create a
[`dictionary`](http://docs.python.org/2/tutorial/datastructures.html#dictionaries)
in python that automatically counts the repetitions of its elements:

when an element that is not contained is added it should insert it with
corresponding value `1`, if the same element is inserted again it will
increment its counter.

Is there a more elegant / compact way to implement this dictionary rather than
the following?

    
    
    if var in myList:
        myDictionary[var] += 1
    else:
        myDictionary[var] = 1
    


Answer: 
    from collections import defaultdict
    d = defaultdict(int)
    d[var] += 1
    

