Collections Module — Counter, defaultdict, deque — Free Python Tutorial
Learn Collections Module — Counter, defaultdict, deque in Python with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
TL;DR: Learn Collections Module — Counter, defaultdict, deque in Python with a free, beginner-friendly tutorial, examples and practice for Indian students on…
Written & reviewed by the Syllab.in Academic Team (CBSE/NCERT subject experts) · Updated
Collections Module — Counter, defaultdict, deque in Python
The collections module has specialised containers more efficient than plain dict/list for common patterns.
Counter: auto-counts elements. Perfect for word frequency, vote counting, histogram creation. counter.most_common(n) returns top n.
defaultdict(list) creates a dict of lists — no KeyError on first access. deque (double-ended queue) is O(1) for append/pop from both ends, unlike lists (O(n) for left-side operations).
Collections Module — Counter, defaultdict, deque — Syntax
from collections import Counter, defaultdict, deque
c = Counter("banana") # Counter({'a':3,'n':2,'b':1})
dd = defaultdict(list)
dd["fruits"].append("apple") # no KeyError!
dq = deque([1,2,3], maxlen=3) # sliding window
Learn Collections Module — Counter, defaultdict, deque step by step with Syllab's free interactive Python tutorial — runnable code examples, practice exercises and instant AI feedback, all free with no signup. Explore the full Python course →