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 collections: A Buffet of Specialized Data Types
In this tutorial, you'll learn all about the series of specialized container data types in the collections module from the Python standard library.
For additional information on related topics, take a look at the following resources:
- Python's pathlib Module: Taming the File System (Tutorial)
- Python itertools By Example (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)
- Click and Python: Build Extensible and Composable CLI Apps (Tutorial)
- How to Evaluate the Quality of Python Packages (Tutorial)
- Python's collections: A Buffet of Specialized Data Types (Quiz)
- Using Python's pathlib Module (Course)
- Python's pathlib Module: Taming the File System (Quiz)
- Python itertools By Example (Course)
- 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)
- The subprocess Module: Wrapping Programs With Python (Quiz)
- Building Command Line Interfaces With argparse (Course)
- Build Command-Line Interfaces With Python's argparse (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 23, 2026