collections
Use Counter, defaultdict, and namedtuple for specialized data handling
Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.
Beyond basic lists and dicts
Python's built-in list and dict cover most needs. But some tasks require counting items, grouping data, or creating lightweight data objects. Writing this logic by hand is repetitive and error-prone.
The collections module provides specialized containers that solve these common patterns in one line.
Counter: count things instantly
In the Data Structures course, you wrote loops to count word frequencies. Counter does the same thing without the loop:
from collections import Counter
words = ["apple", "banana", "apple", "cherry", "banana", "apple"]
counts = Counter(words)
print(counts) # Counter({'apple': 3, 'banana': 2, 'cherry': 1})Counter is a dictionary subclass. You access counts like a regular dict: counts["apple"] returns 3. If a key doesn't exist, it returns 0 instead of raising a KeyError.
The most_common() method returns items sorted by frequency:
counts.most_common(2) # [('apple', 3), ('banana', 2)]defaultdict: dictionaries with default values
A regular dict raises KeyError when you access a missing key. defaultdict creates the key automatically with a default value:
from collections import defaultdict
groups = defaultdict(list)
groups["fruit"].append("apple")
groups["fruit"].append("banana")
groups["veggie"].append("carrot")
print(dict(groups)) # {'fruit': ['apple', 'banana'], 'veggie': ['carrot']}The argument to defaultdict is a function that creates the default value. list creates an empty list. int creates 0. set creates an empty set.
Without defaultdict, you would need an if key not in dict check before every access. defaultdict eliminates that boilerplate.
namedtuple: lightweight data objects
A regular tuple stores values by position. You access them with indices like point[0] and point[1]. This works, but the code is hard to read — what does index 0 mean?
namedtuple creates tuple subclasses with named fields:
from collections import namedtuple
Point = namedtuple("Point", ["x", "y"])
p = Point(3, 4)
print(p.x) # 3
print(p.y) # 4Named tuples are immutable (like regular tuples) but readable (like dictionaries). Use them for small, fixed data structures — coordinates, RGB colors, database records.
Instructions
Use Counter, defaultdict, and namedtuple to process data.
- Import
Counter,defaultdict, andnamedtuplefromcollections. - Create a variable named
wordsand assign it the list["python", "java", "python", "rust", "java", "python", "go", "rust"]. - Create a variable named
word_countsand assign itCounter(words). - Call
print()withword_counts.most_common(3). - Create a variable named
scoresand assign itdefaultdict(list). Callscores["math"].append(95). Callscores["math"].append(87). Callscores["science"].append(92). - Call
print()withdict(scores). - Create a
namedtupletype namedStudentwith fields"name","grade","gpa". Assign it to a variable namedStudent. - Create a variable named
sand assign itStudent("Alice", 10, 3.8). - Call
print()withf"{s.name}: Grade {s.grade}, GPA {s.gpa}".
from pathlib import Path
import os
current = Path.cwd()
print(f"Current directory: {current}")
home = Path.home()
print(f"Home directory: {home}")
test_path = Path("example.txt")
print(f"Name: {test_path.name}")
print(f"Stem: {test_path.stem}")
print(f"Suffix: {test_path.suffix}")
from datetime import datetime, date, timedelta
now = datetime.now()
print(now.strftime("%Y-%m-%d %H:%M"))
birthday = date(2000, 6, 15)
today = date.today()
age_days = (today - birthday).days
print(f"Days since birthday: {age_days}")
next_week = today + timedelta(days=7)
print(f"One week from today: {next_week.strftime('%B %d, %Y')}")
# Step 1: Import Counter, defaultdict, and namedtuple from collections
# Step 2: Create the words list
# Step 3: Count the words with Counter
# Step 4: Print the 3 most common words
# Step 5: Create a defaultdict of lists, add scores
# Step 6: Print the scores dict
# Step 7: Create a Student namedtuple type
# Step 8: Create a student instance
# Step 9: Print the student info
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding