Exploring Counter and defaultdict in python collections

I needed to count frequency of items in a list. Given a list like below

some_data = ['a','b','v','c','l','c']

The ugly way was using the dictionary

frequency_count = dict()
some_data = ['a','b','v','c','l','c']
for item in some_data:
if item in frequency_count:
frequency_count[item] += 1
else:
frequency_count[item] = 1
print frequency_count
view raw freq_dict hosted with ❤ by GitHub

The end result is
{'a': 1, 'c': 2, 'b': 1, 'l': 1, 'v': 1}

But we can use Counter which is much cleaner

from collections import Counter
cnt = Counter()
some_data = ['a','b','v','c','l','c']
for item in some_data:
cnt[item] += 1
print cnt

Result -

Counter({'c': 2, 'a': 1, 'b': 1, 'l': 1, 'v': 1})

But, more elegant, as suggested in comments by Andy

from collections import Counter
some_data = ['a','b','v','c','l','c']
result = Counter(some_data)
view raw gistfile1.txt hosted with ❤ by GitHub

And, another way is using defaultdict

from collections import defaultdict
some_data = ['a','b','v','c','l','c']
frequency_count = defaultdict(int)
for item in some_data:
frequency_count[item] += 1
print frequency_count
view raw freq_defaultdict hosted with ❤ by GitHub

Result

defaultdict(, {'a': 1, 'c': 2, 'b': 1, 'l': 1, 'v': 1})

You can read more here http://docs.python.org/2/library/collections.html