Collections

Master Python's most powerful standard library modules for building high-performance, memory-efficient code.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

🧰 What You'll Learn

This lesson covers incredibly powerful modules that advanced developers rely on every day:

📥 Python Download & Setup

Part 1: Core Modules — collections, itertools, functools

1. collections — High-Performance Container Tools

The collections module provides optimized data structures that outperform normal lists/dicts in many use cases.

1.1 Counter — Counting Made Easy

Best for: word frequency, counting events, histograms, text processing

1.2 defaultdict — Automatic Missing Values

1.3 deque — Fast Queue / Stack / Sliding Window

1.4 namedtuple — Lightweight, Readable Structs

1.5 OrderedDict (for deterministic ordering)

Mostly replaced by Python 3.7+ dict, but still useful for:

1.6 ChainMap — Layered Configuration

2. itertools — High-Performance Iterator Recipes

itertools is one of Python's strongest modules. It eliminates heavy loops and enables efficient pipelines.

2.1 Infinite Iterators

2.2 Combinatorics (product, permutations, combinations)

2.3 accumulate — Cumulative Operations

2.4 groupby — Group Consecutive Items

2.5 islice — Slice Iterators Without Lists

2.6 chain — Combine Multiple Iterables

2.7 tee — Duplicate Iterators

3. functools — Functional Power Tools

3.1 lru_cache — Instant Caching

3.2 partial — Pre-Fill Function Arguments

3.3 reduce — Functional Reduce Operations

3.4 singledispatch — Generic Functions

3.5 cached_property — Lazy Loaded Attributes

4. Combining All Three Modules Into Powerful Pipelines

Part 2: Advanced Patterns With Each Module

1. Advanced Counter Patterns

1.1 Subtracting / Combining Counters

2. Advanced defaultdict Usage

2.1 Multi-Level defaultdict

3. Advanced deque Patterns

3.1 Efficient Sliding Window Statistics

4. Advanced namedtuple Usage

5. Advanced itertools Patterns

5.1 Batch Iteration (Chunking)

5.2 Combining Streams With zip_longest

6. Advanced functools Patterns

6.1 Combining partial + map

Part 3: High-Performance Data Pipelines

8. High-Performance Data Pipelines (Full Walkthrough)

Modern Python systems — data processing apps, ML pipelines, scrapers, log processors — rely on streaming, chunking, and lazy evaluation.

Complete Pipeline Example

🎓 Final Summary

In this comprehensive lesson, you learned expert-level usage of three critical Python modules:

📋 Quick Reference — Advanced Collections

Tool

Best for

Counter(iterable)

Count occurrences of elements

deque(maxlen=100)

Fast append/pop from both ends

defaultdict(list)

Dict with automatic default values

itertools.chain(*iterables)

Combine multiple iterables

functools.lru_cache

Cache function results by arguments

You can now use Counter, deque, defaultdict, itertools and functools to write more expressive and efficient Python.

Up next: Functional Programming — master map, filter, reduce, and pure function patterns.

Practice quiz

What is collections.Counter best suited for?

  • Sorting a list
  • Removing duplicates only
  • Counting occurrences of elements (frequencies)
  • Reversing a string

Answer: Counting occurrences of elements (frequencies). Counter counts occurrences — ideal for word frequencies, histograms, and event counting.

What does a defaultdict(list) give you when you access a missing key?

  • An automatically created empty list
  • A KeyError
  • None
  • An empty string

Answer: An automatically created empty list. defaultdict(list) creates a new empty list for any missing key, so you can append without checking.

What is a deque and what is its key advantage?

  • A sorted set with O(log n) lookup
  • A read-only tuple
  • A hash map with default values
  • A double-ended queue with O(1) append/pop on both ends

Answer: A double-ended queue with O(1) append/pop on both ends. deque is a double-ended queue offering O(1) appends and pops from both ends.

After dq = deque(maxlen=5) and appending 0..9 one at a time, what does list(dq) contain?

A maxlen deque discards items from the opposite end, keeping the last 5: [5,6,7,8,9].

What does namedtuple give you over a plain tuple?

  • Named fields with tuple speed and immutability
  • Mutability
  • Automatic sorting
  • Default dictionary behavior

Answer: Named fields with tuple speed and immutability. namedtuple adds readable field names while keeping tuple speed, immutability, and memory efficiency.

What does functools.lru_cache do to a function?

  • Runs it in parallel
  • Logs every call
  • Caches results keyed by arguments to avoid recomputation
  • Makes it asynchronous

Answer: Caches results keyed by arguments to avoid recomputation. lru_cache stores results by argument, returning the cached value on repeat calls.

What does functools.partial(multiply, 10) produce?

  • A copy of multiply
  • A new callable with the first argument pre-filled as 10
  • An error
  • The number 10

Answer: A new callable with the first argument pre-filled as 10. partial pre-fills arguments, so partial(multiply, 10)(5) calls multiply(10, 5).

What is list(itertools.accumulate([1, 2, 3, 4]))?

accumulate produces a running sum by default: 1, 1+2, 1+2+3, 1+2+3+4.

What does itertools.chain([1,2], [3,4], [5]) yield?

chain links multiple iterables into one continuous sequence: 1, 2, 3, 4, 5.

What does itertools.groupby group together?

  • All equal items anywhere in the iterable
  • Items by their hash value
  • Only consecutive equal items
  • Items into pairs

Answer: Only consecutive equal items. groupby groups only consecutive equal items, so unsorted input may produce repeated keys.

Continue this course