standard library

The standard library should be your first stop. It’s widely available, well-tested, and documented in a consistent way. When a standard library module solves your problem, you avoid adding extra dependencies and you make your code easier to run in new environments.

For standard library modules, these best practices help you get the most out of what Python ships with:

  • Reach for the standard library first: Check whether a standard library module already solves your problem before writing your own solution.
  • Prefer standard tools over ad-hoc code: If a module expresses your intent clearly (for example, collections.Counter for counting or pathlib for paths), use it instead of reimplementing common patterns.
  • Use the right abstraction level: When a module offers a high-level API, prefer it over lower-level building blocks unless you need the extra control.
  • Lean on documentation and examples: The standard library is big. When you’re unsure, check the docs and try a small example in a REPL.

To see how the standard library can replace ad-hoc code, say that you need to count the number of times a word appears in a list:

🔴 Avoid this:

Python
>>> words = ["python", "pep8", "python", "testing"]
>>> counts = {}

>>> for word in words:
...     if word in counts:
...         counts[word] += 1
...     else:
...         counts[word] = 1
...

>>> counts
{'python': 2, 'pep8': 1, 'testing': 1}

This code works, but it manually reimplements a common pattern: counting the frequency of each item in a sequence. The standard library already solved this for you.

Favor this:

Python
>>> from collections import Counter

>>> words = ["python", "pep8", "python", "testing"]
>>> counts = Counter(words)

>>> counts
Counter({'python': 2, 'pep8': 1, 'testing': 1})

In this version, you use Counter from the collections module. The result is shorter, clearer, and more readable. You don’t need to maintain your own counting logic, and other developers reading your code can immediately recognize your intent.

Tutorial

Python's pathlib Module: Taming the File System

Python's pathlib module enables you to handle file and folder paths in a modern way. This built-in module provides intuitive semantics that work the same way on different operating systems. In this tutorial, you'll get to know pathlib and explore common tasks when interacting with paths.

intermediate python stdlib

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Feb. 3, 2026