from collections import Counter # Count frequency of items in a list my_list = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'] counts = Counter(my_list) print(f"Item counts: {counts}") # Expected: Counter({'apple': 3, 'banana': 2, 'orange': 1}) # Count frequency of characters in a string my_string = "hello world" char_counts = Counter(my_string) print(f"Character counts: {char_counts}") # Expected: Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1}) # Find the most common elements most_common_items = counts.most_common(2) # Top 2 most common print(f"Most common 2 items: {most_common_items}") # Expected: [('apple', 3), ('banana', 2)] # Arithmetic operations with Counters c1 = Counter('aabbc') c2 = Counter('abbccc') print(f"c1 + c2: {c1 + c2}") # Combines counts print(f"c1 - c2: {c1 - c2}") # Subtracts counts (keeps positive)