Python
Named Tuples
Python Programming
Coding
Programming Concepts

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.

Browse interview questions

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.

python
point = (10, 20)
print(point[0], point[1])

That is valid, but it is not very descriptive. A named tuple makes the same data self-documenting.

python
1from collections import namedtuple
2
3Point = namedtuple("Point", ["x", "y"])
4p = Point(10, 20)
5
6print(p.x, p.y)
7print(p[0], p[1])

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.

python
1from collections import namedtuple
2
3Employee = namedtuple("Employee", ["name", "department", "salary"])
4emp = Employee("Alice", "Finance", 75000)
5
6print(emp.name)
7print(emp.department)
8print(emp.salary)

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.

python
record = Employee("Bob", "IT", 82000)
print(record[0])      # Bob
print(record.name)    # Bob

Useful built-in helper methods

Named tuple instances and classes provide a few helpful methods.

python
1from collections import namedtuple
2
3Point = namedtuple("Point", ["x", "y"])
4p = Point(1, 2)
5
6print(p._asdict())
7print(Point._make([3, 4]))
8print(p._replace(y=99))

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

python
1from collections import namedtuple
2
3Result = namedtuple("Result", ["filename", "line_count", "has_errors"])
4
5
6def inspect_file(name, text):
7    return Result(name, len(text.splitlines()), "ERROR" in text)
8
9res = inspect_file("app.log", "INFO start\nERROR fail\n")
10print(res.filename)
11print(res.line_count)
12print(res.has_errors)

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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.