Siavash Khallaghi     About     Archive

Daily Python Snippet 1

I find these Python snippets more pythonic than their alternative.

Counting Elements in a List

Good:

x = ['a', 'a', 'b', 'c', 'd', 'c', 'a']
count = {}
for e in x:
    if e not in count.keys():
        count[e] = 1
    else:
        count[e] += 1

Better:

x = ['a', 'a', 'b', 'c', 'd', 'c', 'a']
count = {}
for e in x:
    count[e] = count.get(e, 0) + 1

Best:

from collections import Counter

x = ['a', 'a', 'b', 'c', 'd', 'c', 'a']
count = Counter(x)
comments powered by Disqus