Find the most common item in a sequence of items

Here’s a puzzler posed to me by Chris Laffra, one of the Bank of America Python gurus: Given a finite sequence of objects which contain some repetition find the most common object.

Here’s an example followed by my own naive solution:


input = 'aaabaccabdaabacccabdagatacaccacba'

counts = [(input.count(x), x) for x in set(input)]
sortedCounts = sorted(counts, key=lambda x:-x[0])
print sortedCounts[0][1]

This is pretty basic stuff, and the only thing I can say in defence of this first solution is that I bashed it out quickly. But after a few minutes of pondering I managed to find the optimal solution:


print max(input, key=input.count)

That’s only 18 bytes, not including whitespace and the print statement. This is a lovely example of what makes Python truly expressive – the ability to say a great deal in very few keystrokes.

TIL: You can assign to list slices

An oddity of Python brought to my attention by Daniel Pope:

foo = range(10)
print(foo)
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

foo[2:-2] = ["stoat", "ferret", "wombat"]
print(foo)
# [0, 1, 'stoat', 'ferret', 'wombat', 8, 9]

foo[:] = []
print(foo)
# []