How to count top 10 most common values in a dict in python

Initialize the Counter once, let the keys be artists, and augment a key (artist) each time through the loop:

c = Counter()
for d in entries:
    arts = d['artist']
    c[arts] += 1
print(c.most_common(10))

When arts is a string, then c = Counter(arts) counts the characters in arts:

In [522]: collections.Counter('Led Zepplin')
Out[522]: Counter({'e': 2, 'p': 2, ' ': 1, 'd': 1, 'i': 1, 'L': 1, 'l': 1, 'n': 1, 'Z': 1})

In contrast:

In [523]: c = collections.Counter()

In [524]: c['Led Zepplin'] += 1

In [525]: c['The Rolling Stones'] += 1

In [526]: c.most_common()
Out[526]: [('Led Zepplin', 1), ('The Rolling Stones', 1)]

Alternatively, as Jon Clements points out, build a list of all the artists, and then count the list:

c = Counter(d['artist'] for d in entries)
print(c.most_common(10))

Note that the above uses a generator expression to avoid building a (possibly) large temporary list, and at the same time has a much more succinct, readable syntax.

Leave a Comment