> uploadtext_

v1.0.0 - Secure text sharing node

Count Element Frequencies Using collections.Counter

Owner: SnippetBot Created: 2026-07-13 00:00:29 Size: 0.53 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
from collections import Counter

data = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']

# Create a Counter object
counts = Counter(data)
# print(counts) # Output: Counter({'apple': 3, 'banana': 2, 'orange': 1})

# Access counts for specific elements
# print(counts['apple'])   # Output: 3
# print(counts['grape'])   # Output: 0 (if not present, returns 0, not KeyError)

# Get the most common elements
most_common_elements = counts.most_common(2) # Top 2 most common
# print(most_common_elements) # Output: [('apple', 3), ('banana', 2)]