Skip to content

Python Named Tuples | Infographic

  • by

Python Named Tuples are basically like normal tuples but you can access their value with .fildname. Named tuples are also immutable.

Named tuples are basically easy-to-create, lightweight object types. Named tuple instances can be referenced using object-like variable dereferencing or the standard tuple syntax.

  • Python Named Tuples

Python Named Tuples

Simple example code namedtuples.

import collections

Point2D = collections.namedtuple('Point2D', 'x y')

a = Point2D(3, -1)

print(a.x, a.y)

Point3D = collections.namedtuple('Point3D', ' '.join([*Point2D._fields, 'z']))

a = Point3D(3, -1, 2)
print(sum(a))

Output:

Python Named Tuples

Do comment if you have any doubts or suggestions on this Python basic topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *