namedtuple – A Powerful Tool for Data Manipulation in Python 2026
collections.namedtuple creates lightweight, immutable, readable data structures that combine the best of tuples and classes. In 2026 it remains one of the most elegant and performant tools for clean data manipulation, especially when working with records, API responses, and configuration data.
TL;DR — Why namedtuple is Powerful
- Readable attribute access instead of index access
- Immutable (safe for hashing and functional style)
- Lightweight — uses less memory than regular classes
- Built-in methods like
_asdict(),_replace(), and_make()
1. Basic Creation and Usage
from collections import namedtuple
# Define a namedtuple
Point = namedtuple("Point", ["x", "y"])
p = Point(10, 20)
print(p.x, p.y) # 10 20
print(p[0], p[1]) # also works as tuple
print(p) # Point(x=10, y=20)
2. Real-World Data Manipulation Examples
# Working with CSV / API data
User = namedtuple("User", ["id", "name", "email", "active"])
users = [
User(1, "Alice", "alice@example.com", True),
User(2, "Bob", "bob@example.com", False)
]
# Clean attribute access
active_users = [u for u in users if u.active]
emails = [u.email for u in users]
# Convert to dict for JSON
user_dicts = [u._asdict() for u in users]
print(user_dicts)
3. Advanced Features – _replace() and _make()
# Create new instance with updated values (immutable)
updated_user = users[0]._replace(active=False, name="Alice Smith")
# Create from iterable
data = [101, "Charlie", "charlie@example.com", True]
new_user = User._make(data)
4. Best Practices in 2026
- Use
namedtuplefor simple immutable records and data transfer objects - Prefer
dataclass(withfrozen=True) when you need methods or default values - Great for CSV processing, API responses, and configuration records
- Combine with type hints for better IDE support and static analysis
Conclusion
namedtuple is a lightweight, readable, and immutable tool that makes data manipulation cleaner and safer. In 2026 it continues to be the go-to choice when you need tuple-like performance with named attributes. Use it freely for records and data pipelines, and switch to dataclass only when you need more complex behavior.
Next steps:
- Replace your list-of-lists or list-of-dicts with namedtuples for better readability