The beauty of Python lies not just in its simplicity, but in the power it offers out-of-the-box. While you might be familiar with modules like os, json, or math, the real gems often lie buried — overlooked yet incredibly powerful. In this article, we’ll explore 5 underrated Python standard library modules that can supercharge your productivity and simplify your development workflow.
Introduction
What are Python library modules?
A Python module is a file containing Python definitions and statements. A module can define functions, classes, and variables. A module can also include runnable code. Grouping related code into a module makes the code easier to understand and use. It also makes the code logically organized.
In programming, standard modules are pre-built sets of functions, procedures, or code blocks that provide reusable functionality. They are often included as part of a programming language’s standard library or as pre-defined components within a particular framework or software tool.
What is Python standard library?
The Python standard library is a collection of pre-written modules that come bundled with every Python installation. These modules provide a wide range of functionalities and tools, allowing developers to perform various tasks without the need for external installations. It covers areas like file handling, networking, data manipulation, mathematics, and more.
These modules are built-in — no pip installs, no setup headaches — just pure Python magic. Let’s dive in!
1. collections: Smarter Data Structures
The collections module is like a Swiss Army knife for Python developers. It offers specialized container datatypes that go far beyond list, dict, and tuple.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
1.1 defaultdict: Never Worry About Missing Keys Again
Tired of checking if a key exists before appending to a list in a dictionary? Enter defaultdict.
from collections import defaultdict
cities_by_state = defaultdict(list)
data = [
("Los Angeles", "CA"),
("San Francisco", "CA"),
("Dallas", "TX"),
("Austin", "TX"),
("New York", "NY")
]
for city, state in data:
cities_by_state[state].append(city)
print(cities_by_state["CA"]) # ['Los Angeles', 'San Francisco']
print(cities_by_state["FL"]) # []
Why it’s a gem: Avoids KeyErrors, and leads to cleaner, more Pythonic code.
1.2 namedtuple: Tuples With Names, Not Confusion
Need the speed of tuples but the readability of objects? That’s where namedtuple shines.
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(10, 20)
print(p.x, p.y) # 10 20
Use it when: You’re working with fixed-structure data like coordinates, records, or logs.
2. functools: Function Wizardry
The functools module is a goldmine for functional programming fans. Whether you want to optimize performance or simplify callbacks, this module delivers.
2.1 lru_cache: Add Memoization in One Line
Need to speed up recursive functions like Fibonacci or pathfinding? lru_cache has your back.
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
if n <= 2:
return 1
return fib(n-1) + fib(n-2)
print(fib(100)) # Efficiently computed
Why use it: It dramatically boosts performance in recursive or repeated computations.
2.2 partial: Pre-Fill Function Arguments
Ever wanted to reuse a function with some preset arguments? partial makes it easy.
from functools import partial
def multiply(x, y):
return x * y
double = partial(multiply, 2)
print(double(5)) # 10
Perfect for: Simplifying APIs, callbacks, or reusable configs in apps.
Use case: Creating card games, simulations, or test case permutations.
3.2 groupby: Group Like a SQL Query
from itertools import groupby
animals = ['cat', 'cat', 'dog', 'dog', 'elephant']
for key, group in groupby(animals):
print(key, list(group))
Heads-up: Your data needs to be sorted before grouping.
4. re: Mastering Text with Regular Expressions
Text processing? Data cleaning? Log analysis? The re module is a powerful, built-in regex engine.
4.1 findall: Match Everything You Need
import re
emails = re.findall(r'\b[\w.-]+@[\w.-]+\.\w+\b', "Contact me at test@example.com or hello@site.io")
print(emails) # ['test@example.com', 'hello@site.io']
4.2 sub: Replace in Style
text = "My number is 123-456-7890"
masked = re.sub(r'\d', '*', text)
print(masked) # My number is ***-***-****
Why use it: Perfect for anonymizing data or scrubbing inputs.
5. pathlib: The Modern Way to Handle File Paths
Forget os.path. Python 3’s pathlib makes file and directory handling object-oriented and elegant.
5.1 Path: Cross-platform File Magic
from pathlib import Path
file = Path("sample.txt")
if file.exists():
print(file.read_text())
5.2 PurePath: Paths Without Touching the Filesystem
from pathlib import PurePath
path = PurePath("folder/subfolder/file.txt")
print(path.parent) # folder/subfolder
Why it’s awesome: Clean, readable, and intuitive file operations across Linux, macOS, and Windows.