Python Tip of the Day: Specialized Containers with collections Module
Specialized Containers with collections
Module
Author: Jeremy Morgan
Published: November 12, 2024
I wrote a book! Check out A Quick Guide to Coding with AI.
Become a super programmer!
Learn how to use Generative AI coding tools as a force multiplier for your career.
Need something more than a regular list or dict? collections
has you covered.
# Count occurrences with Counter
from collections import Counter
colors = ['red', 'blue', 'red', 'green', 'blue', 'blue']
color_counts = Counter(colors)
print(color_counts)
# Output: Counter({'blue': 3, 'red': 2, 'green': 1})
The Python Tip of the Day is a daily series published in the month of November. The tips are designed to help you become a better Python programmer. I post tips like this and more every single day on X. Let’s connect!
I wrote a book! Check out A Quick Guide to Coding with AI.
Become a super programmer!
Learn how to use Generative AI coding tools as a force multiplier for your career.