# sorting - How to sort Counter by value? - python

ID : 20250

viewed : 10

### Top 5 Answer for sorting - How to sort Counter by value? - python  95

Use the `Counter.most_common()` method, it'll sort the items for you:

``>>> from collections import Counter >>> x = Counter({'a':5, 'b':3, 'c':7}) >>> x.most_common() [('c', 7), ('a', 5), ('b', 3)] ``

It'll do so in the most efficient manner possible; if you ask for a Top N instead of all values, a `heapq` is used instead of a straight sort:

``>>> x.most_common(1) [('c', 7)] ``

Outside of counters, sorting can always be adjusted based on a `key` function; `.sort()` and `sorted()` both take callable that lets you specify a value on which to sort the input sequence; `sorted(x, key=x.get, reverse=True)` would give you the same sorting as `x.most_common()`, but only return the keys, for example:

``>>> sorted(x, key=x.get, reverse=True) ['c', 'a', 'b'] ``

or you can sort on only the value given `(key, value)` pairs:

``>>> sorted(x.items(), key=lambda pair: pair, reverse=True) [('c', 7), ('a', 5), ('b', 3)] ``  88

A rather nice addition to @MartijnPieters answer is to get back a dictionary sorted by occurrence since `Collections.most_common` only returns a tuple. I often couple this with a json output for handy log files:

``from collections import Counter, OrderedDict  x = Counter({'a':5, 'b':3, 'c':7}) y = OrderedDict(x.most_common()) ``

With the output:

``OrderedDict([('c', 7), ('a', 5), ('b', 3)]) {   "c": 7,    "a": 5,    "b": 3 } ``  70

Yes:

``>>> from collections import Counter >>> x = Counter({'a':5, 'b':3, 'c':7}) ``

Using the sorted keyword key and a lambda function:

``>>> sorted(x.items(), key=lambda i: i) [('b', 3), ('a', 5), ('c', 7)] >>> sorted(x.items(), key=lambda i: i, reverse=True) [('c', 7), ('a', 5), ('b', 3)] ``

This works for all dictionaries. However `Counter` has a special function which already gives you the sorted items (from most frequent, to least frequent). It's called `most_common()`:

``>>> x.most_common() [('c', 7), ('a', 5), ('b', 3)] >>> list(reversed(x.most_common()))  # in order of least to most [('b', 3), ('a', 5), ('c', 7)] ``

You can also specify how many items you want to see:

``>>> x.most_common(2)  # specify number you want [('c', 7), ('a', 5)] ``  63

More general sorted, where the `key` keyword defines the sorting method, minus before numerical type indicates descending:

``>>> x = Counter({'a':5, 'b':3, 'c':7}) >>> sorted(x.items(), key=lambda k: -k)  # Ascending [('c', 7), ('a', 5), ('b', 3)] ``  52