def get_topk(l, k): counts = {} for i in l: n = int(i) counts[n] = counts.setdefault(n, 0) + 1 h = [(count,n) for (n,count) in counts.items()] h.sort(reverse=True) return h[:k]