How to sort a Counter object by count in Python

A collections.Counter object is a collection for counting objects in a sequence. The Counter object stores unique objects with their corresponding counts. For example, a Counter object constructed from the list [“a”, “b”, “b”] is {“b: 2, “a”: 1}.

Solution for How to sort a Counter object by count in Python : You can use Counter.most_common() to sort a Counter object by count Call Counter.most_common() on a Counter object to return a descending list of tuples. Each tuple represents an object from the sequence paired with its corresponding count.


how-to-sort-a-counter-object-by-count-in-python