Collection Functions

collections.namedtuple()

Generate a tuple subclass with named, read-only fields, normal tuple indexing and optional defaults for rightmost fields.

Formal Syntax & Structure

python syntaxpython
collections.namedtuple(typename, field_names, *, rename=False, defaults=None, module=None)

Parameters & Attributes

NameTypeDescription
typenamestrName of the generated tuple subclass.
field_namesiterable[str] or strField names, either iterable or separated by spaces or commas.
renameboolAutomatically rename invalid or duplicate fields when true.
defaultsiterable or NoneDefault values for the rightmost fields.
modulestr or NoneModule name assigned to the generated type.

Return Value

type

The type result described above.

Working Example

Example Snippetpython
import collectionsPoint=collections.namedtuple("Point","x y")p=Point(3,4)print((p.x,p[1],tuple(p)))
Python Reference