Why would you choose the O (n ** 2) algorithm for this. An alternative to a counter (if you have <2.7) is not too complicated
>>> from operator import itemgetter >>> from collections import defaultdict >>> L=[3, 3, 3, 4, 4, 2] >>> D=defaultdict(int) >>> for i in L: ... D[i]+=1 ... >>> sorted(D.items(), key=itemgetter(1), reverse=True) [(3, 3), (4, 2), (2, 1)]
John la rooy
source share