xxxxxxxxxx
#The Counter() function returns a dictionary which is unordered.
#You can sort it according to the number of counts in each element
#using most_common() function of the Counter object.
list = [1,2,3,4,1,2,6,7,3,8,1]
cnt = Counter(list)
print(cnt.most_common())
#Output: [(1, 3), (2, 2), (3, 2), (4, 1), (6, 1), (7, 1), (8, 1)]
#You can see that most_common function returns a list,
#which is sorted based on the count of the elements.
#1 has a count of three, therefore it is the first element of the list.
xxxxxxxxxx
from collections import Counter
# Create a sample list of elements
my_list = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
# Count the occurrences of each element
counter = Counter(my_list)
# Retrieve the two most common elements and their counts
most_common_items = counter.most_common(2)
# Print the result
for item, count in most_common_items:
print(item, count)