Collections

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

Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.

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.

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

1. collections — High-Performance Container Tools

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

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

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

2. itertools — High-Performance Iterator Recipes

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

3. functools — Functional Power Tools

4. Combining All Three Modules Into Powerful Pipelines

1. Advanced Counter Patterns

2. Advanced defaultdict Usage

3. Advanced deque Patterns

4. Advanced namedtuple Usage

5. Advanced itertools Patterns

6. Advanced functools Patterns

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.

🎓 Final Summary

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

📋 Quick Reference — Advanced Collections

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.