You often see frequency counts done with dicts, requiring serveral lines of code. Here is a way to do it in one line using itertools and list comprehensions. This revised version was suggested by Raymon Hettinger. It is O(n log n).
1 2 3
>>> from itertools import groupby >>> [(k, len(list(g))) for k, g in groupby(sorted(myList))] [('1', 4), ('2', 1), ('3', 2), ('4', 1)]