Skip to content

Reach for the standard library first

I can recognise when a task is already solved in Python's standard library, find the module that solves it, and use it instead of writing the loop myself.

Domain
Python Programming
Level
intermediate
Status
draft · v0.1.0
Also called
Standard library, collections, itertools, datetime, Batteries included
License
CC BY-SA 4.0

Learn first

Learn it

Scope

The habit of asking “is this already solved?” before writing a loop, and knowing enough of the library to answer.

In scope: collections, itertools and datetime as the three that pay back soonest, plus finding a module you have not used. Out of scope: third-party packages, and the library’s more specialised corners.

Why this is a skill and not a lookup

The library is large enough that nobody holds it in their head. What experienced Python programmers have is a suspicion: this counting loop feels like something that exists. That suspicion is what sends them to the index, and it is what this outcome builds.

The payoff is not brevity. It is that Counter(words).most_common(3) cannot have an off-by-one error, and the loop you would have written can.

The three that pay back soonest

  • collections — Counter for tallying, defaultdict for grouping without checking whether the key exists, deque when you add and remove at both ends. Grouping with defaultdict(list) replaces the most-written five lines in beginner Python.
  • itertools — building blocks over iterables: chain to treat several sequences as one, groupby for runs in sorted data, combinations and product for pairings you would otherwise nest loops for, accumulate for running totals. They are lazy, so they compose over large data.
  • datetime — dates and times as objects with arithmetic, not strings. Parse with fromisoformat or strptime, format with isoformat or strftime.

The datetime warning

A datetime with no timezone is naive: it names a wall clock with no claim about where. Comparing a naive datetime with an aware one raises, and comparing two naive ones from different places is silently wrong. Store and compute in UTC, attach a timezone as soon as you know one, and convert only when showing a person.

Common misconceptions

  • “Writing it myself is clearer.” For counting, grouping and pairing, the named function is the clearer version. Your loop needs a comment; Counter does not.
  • “itertools is for clever code.” It is for not holding a million items in memory. The laziness is the point.
  • “Install a package for that.” Check the library first. A dependency is a permanent cost — updates, security, someone else’s release schedule — paid for a loop you could have avoided.
  • “datetime.now() is the current time.” It is the current time somewhere, unlabelled. datetime.now(timezone.utc) is a fact.

Notes for teachers

Give learners working code that counts and groups by hand, and ask them to shorten it using only the standard library. Then ask which version they would rather debug at midnight.

A good habit to set: before writing any loop over ten lines, open the library index and spend one minute looking. The minute usually pays.

Resources

ResourceKindCostProvider
The Python Tutorial: a brief tour of the standard libraryreadingfreePython Software Foundation
collections: container datatypesreferencefreePython Software Foundation
itertools: functions creating iterators for efficient loopingreferencefreePython Software Foundation
datetime: basic date and time typesreferencefreePython Software Foundation

Courses that include it

draft

Python for a Coding Assessment

A refresher path through the Python graph for someone who has written Python before and has an assessment coming: run and read, rebuild the core, then work the way an assessment expects.

Prove it

Evidence of mastery

  • Replaces hand-written counting or grouping code with `collections.Counter` or `defaultdict`, and the result is shorter and clearer.
  • Uses an `itertools` function to express a pairing, grouping or running calculation that would otherwise need nested loops.
  • Parses and formats dates with `datetime` without writing string arithmetic, and explains why a naive datetime is a risk.
  • Finds the right module for an unfamiliar task from the library index, rather than reaching for a third-party package first.

Assessments that measure it

No assessment yet.

Credentials that include it

No credential yet.

What learners show

Nothing to show yet. The publisher last published on 2026-09-20, and no aggregate for this outcome has reached the 50 learners a number needs. Open Degree does not track learners. About learner evidence.

What comes next

No outcome builds on this yet. Ask for the next concept.