Sort Counter by value


This program uses the Counter class from the collections module in Python. It creates a Counter object x using the dictionary {'Tamil':92, 'Math':88,'Science':89, 'Social':89, 'English':56}. The most_common method is then used on the Counter object, which returns a list of tuples where each tuple is a key-value pair from the Counter object in the order of their count, from the most common to the least. The returned list contains the elements and their count in the Counter object.

Source Code

from collections import Counter
x = Counter({'Tamil':92, 'Math':88,'Science':89, 'Social':89, 'English':56})
print(x.most_common())

Output

[('Tamil', 92), ('Science', 89), ('Social', 89), ('Math', 88), ('English', 56)]