Welcome to Day 32 of our Python journey!
Today, we explore the collections module — a treasure chest of powerful, high-performance data structures beyond standard dict, list, tuple, and set.
Let’s unpack the most useful tools in this module with simple explanations and examples. 🧠
  
  
  📦 What is the collections Module?
The collections module is a built-in Python library that provides specialized container datatypes.
These can help you write cleaner, faster, and more efficient code.
We’ll focus on five key members:
- Counter
- defaultdict
- OrderedDict
- deque
- namedtuple
  
  
  1. 🔢 Counter – Counting Made Easy
The Counter class counts how many times each element appears.
✅ Example:
from collections import Counter
fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
count = Counter(fruits)
print(count)
🧪 Output:
Counter({'apple': 3, 'banana': 2, 'orange': 1})
You can access individual counts:
print(count['apple'])  # 3
  
  
  2. 🧰 defaultdict – Avoid Key Errors
A defaultdict lets you set a default type for missing keys — no need to check if the key exists.
✅ Example:
from collections import defaultdict
grades = defaultdict(int)
grades['Alice'] += 1
grades['Bob'] += 5
print(grades)
🧪 Output:
defaultdict(<class 'int'>, {'Alice': 1, 'Bob': 5})
You can also use list, set, or even a custom function as the default.
  
  
  3. 📜 OrderedDict – Remember Insertion Order (Pre-Python 3.7)
In older versions of Python (before 3.7), normal dict did not preserve insertion order.
OrderedDict ensured that behavior.
✅ Example:
from collections import OrderedDict
od = OrderedDict()
od['a'] = 1
od['b'] = 2
od['c'] = 3
print(od)
🧪 Output:
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
Note: Since Python 3.7+, regular dict also preserves insertion order.
  
  
  4. 🔄 deque – Fast Appends and Pops from Both Ends
Think of deque (double-ended queue) as a flexible list that is optimized for adding/removing items from both ends.
✅ Example:
from collections import deque
dq = deque([1, 2, 3])
dq.append(4)
dq.appendleft(0)
print(dq)  # deque([0, 1, 2, 3, 4])
dq.pop()       # Removes from right
dq.popleft()   # Removes from left
Perfect for queues, stacks, or sliding windows.
  
  
  5. 🧱 namedtuple – Tuples with Named Fields
Create lightweight, readable objects without defining a full class.
✅ Example:
from collections import namedtuple
Person = namedtuple('Person', 'name age')
p = Person('Alice', 30)
print(p.name)  # Alice
print(p.age)   # 30
Acts like a tuple but more self-documenting.
🧠 Summary Table
| Collection | Purpose | Like... | 
|---|---|---|
| Counter | Count items in iterable | Frequency map | 
| defaultdict | Avoid missing key errors | Dict with default values | 
| OrderedDict | Dict with ordered keys (pre-3.7) | Ordered mapping | 
| deque | Fast queue/stack | List (but faster) | 
| namedtuple | Named fields for tuple elements | Lightweight class | 
🔍 Real-World Use Case
Imagine processing log files:
from collections import Counter
with open("access.log") as f:
    ips = [line.split()[0] for line in f]
ip_counts = Counter(ips)
print(ip_counts.most_common(5))  # Top 5 IP addresses
🧪 Practice Task
Try implementing a word counter using Counter:
text = "hello world hello hello code world python code"
# Expected: Counter of each word
 
 
              
 
    
Top comments (0)