Py Collections — Free Python Tutorial
Learn Py Collections in Python with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
TL;DR: Learn Py Collections in Python with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
Written & reviewed by the Syllab.in Academic Team (CBSE/NCERT subject experts) · Updated
Py Collections 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).
Py Collections — 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 Py Collections 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 →