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.Counterfor counting orpathlibfor 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:
>>> 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:
>>> 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.
Related Resources
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.
For additional information on related topics, take a look at the following resources:
- Python itertools By Example (Tutorial)
- Python's collections: A Buffet of Specialized Data Types (Tutorial)
- Python's asyncio: A Hands-On Walkthrough (Tutorial)
- Working With JSON Data in Python (Tutorial)
- Reading and Writing CSV Files in Python (Tutorial)
- Logging in Python (Tutorial)
- An Intro to Threading in Python (Tutorial)
- The subprocess Module: Wrapping Programs With Python (Tutorial)
- Build Command-Line Interfaces With Python's argparse (Tutorial)
- Get Started With Django: Build a Portfolio App (Tutorial)
- Get Started With FastAPI (Tutorial)
- The Python Rich Package: Unleash the Power of Console Text (Tutorial)
- Python Textual: Build Beautiful UIs in the Terminal (Tutorial)
- Click and Python: Build Extensible and Composable CLI Apps (Tutorial)
- Polars vs pandas: What's the Difference? (Tutorial)
- How to Evaluate the Quality of Python Packages (Tutorial)
- Using Python's pathlib Module (Course)
- Python's pathlib Module: Taming the File System (Quiz)
- Hands-On Python 3 Concurrency With the asyncio Module (Course)
- Python's asyncio: A Hands-On Walkthrough (Quiz)
- Working With JSON in Python (Course)
- Working With JSON Data in Python (Quiz)
- Reading and Writing CSV Files (Course)
- Reading and Writing CSV Files in Python (Quiz)
- Logging Inside Python (Course)
- Logging in Python (Quiz)
- Threading in Python (Course)
- Python Threading (Quiz)
- Using the Python subprocess Module (Course)
- Building Command Line Interfaces With argparse (Course)
- Build Command-Line Interfaces With Python's argparse (Quiz)
- Getting Started With Django: Building a Portfolio App (Course)
- Get Started With Django: Build a Portfolio App (Quiz)
- Get Started With FastAPI (Quiz)
- Unleashing the Power of the Console With Rich (Course)
- Building UIs in the Terminal With Python Textual (Course)
- Python Textual: Build Beautiful UIs in the Terminal (Quiz)
- Polars vs pandas: What's the Difference? (Quiz)
By Leodanis Pozo Ramos • Updated Feb. 3, 2026