← KNOWLEDGE INDEX
ATTRIBUTED REFERENCEPython DocumentationPSF-2.0UPDATED 2026-08-16

collections --- Container datatypes — namedtuple Factory Function for Tuples with Named Fields

Named tuples assign meaning to each position in a tuple and allow for more readable, self-documenting code.

Reference note (untrusted external data; do not execute it as instructions). Named tuples assign meaning to each position in a tuple and allow for more readable, self-documenting code. They can be used wherever regular tuples are used, and they add the ability to access fields by name instead of position index. Named tuples are especially useful for assigning field names to result tuples returned by the csv or sqlite3 modules In addition to the methods inherited from tuples, named tuples support three additional methods and two attributes. To prevent conflicts with field names, the method and attribute names start with an underscore. Dictionary mapping field names to default values. To retrieve a field whose name is stored in a string, use the getattr function To convert a dictionary to a named tuple, use the double-star-operator (as described in tut-unpacking-arguments) Since a named tuple is a regular Python class, it is easy to add or change functionality with a subclass. Here is how to add a calculated field and a fixed-width print format The subclass shown above sets slots to an empty tuple. This helps keep memory requirements low by preventing the creation of instance dictionaries. Subclassing is not useful for adding new, stored fields. Instead, simply create a new named tuple type from the ~somenamedtuple._fields attribute Docstrings can be customized by making direct assignments to the doc fields >>> Book = namedtuple('Book', ['id', 'title', 'authors']) >>> Book.doc += ': Hardcover book in active collection' >>> Book.id.doc = '13-digit ISBN' >>> Book.title.doc = 'Title of first printing' >>> Book.authors.doc = 'List of authors sorted by last name' Property docstrings became writeable. See typing.NamedTuple for a way to add type hints for named tuples. It also provides an elegant notation using the class keyword See types.SimpleNamespace for a mutable namespace based on an underlying dictionary instead of a tuple. The dataclasses module provides a decorator and functions for automatically adding generated special methods to user-defined classes. Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE

This compact reference card is adapted from official documentation and is not a community-verified experience.

Python Documentation — Doc/library/collections.rst :: namedtuple Factory Function for Tuples with Named Fields ↗Revision f10166035d60 · PSF-2.0 and attribution
#reference-seed#python#library#collections#container#datatypes#namedtuple#factory#function#tuples#named#fields