What are "named tuples" in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A named tuple is a tuple subtype whose elements can be accessed by name as well as by position. In Python, named tuples are useful when you want the compactness and immutability of a tuple but also want your code to be clearer than raw index-based access.
Why named tuples exist
A plain tuple works well for short positional data, but it becomes hard to read once you stop remembering what each position means.
That is valid, but it is not very descriptive. A named tuple makes the same data self-documenting.
You still get tuple behavior, but the field names make the code much easier to understand.
Creating a named tuple
The classic approach uses collections.namedtuple.
A named tuple type is a real class, so Employee is not just one value. It is a factory for instances with the declared fields.
Important properties
Named tuples are:
- ordered like tuples
- indexable like tuples
- immutable like tuples
- readable through attribute names
That combination is what makes them attractive. They are especially good for lightweight records returned from parsing, queries, or helper functions.
Useful built-in helper methods
Named tuple instances and classes provide a few helpful methods.
These helpers are useful for:
- converting to a dictionary-like structure
- constructing from an iterable
- creating a modified copy without mutating the original
That last point matters because named tuples are immutable. _replace returns a new instance; it does not edit the existing one.
Named tuple versus dataclass
Modern Python also gives you dataclass, so the right question is often when to use one over the other.
Use a named tuple when:
- immutability is desirable
- positional tuple-like behavior is useful
- the structure is small and simple
- you want a very lightweight record type
Use a dataclass when:
- mutability is needed
- defaults and methods are more central
- richer object behavior is expected
- you want type annotations to be the main interface
Named tuples are not obsolete, but they are best for compact record-style data rather than full application models.
A practical example
This is a good fit because the returned object is small, fixed in shape, and easier to use than a bare tuple.
Common Pitfalls
A common mistake is treating a named tuple like a mutable object. It is still a tuple, so fields cannot be assigned in place.
Another mistake is using named tuples for large evolving domain models that really need a dataclass or full class.
A third mistake is forgetting that named tuples still support positional access. That is convenient, but relying heavily on indexes defeats much of the readability benefit.
Summary
- A named tuple is a tuple subtype with named fields.
- It combines tuple efficiency and immutability with clearer attribute access.
- Create one with
collections.namedtuple. - Named tuples work well for small fixed record types.
- Prefer dataclasses or normal classes when the data model is richer or needs mutability.
Related reading
- What are named tuples in Python?
- What are some common uses for Python decorators?
- What are some good Python ORM solutions?
- What are some Python libraries written to demostrate Functional Reactive Programming?
- What are the advantages of NumPy over regular Python lists?
- What are the differences between json and simplejson Python modules?
- What are the differences between numpy arrays and matrices? Which one should I use?
- What are the differences between Pandas and NumPySciPy in Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.