from collections import Counter # Counting words in a sentence sentence = "this is a test this is only a test" words = sentence.split() word_counts = Counter(words) # Result: Counter({'this': 2, 'is': 2, 'a': 2, 'test': 2, 'only': 1}) print(word_counts.most_common(2)) # Result: [('this', 2), ('is', 2)] (order might vary for ties) # Counting items in a list items = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'] item_counts = Counter(items) # Result: Counter({'apple': 3, 'banana': 2, 'orange': 1})