How to use namedtuples

PHOTO EMBED

Tue Jun 18 2024 16:57:24 GMT+0000 (Coordinated Universal Time)

Saved by @pynerds #python

#import the namedtuple factory method
from collections import namedtuple

#create a namedtuple to represent a Point
Point = namedtuple('Point', ['x', 'y'])

#Create objects from the namedtuple 'Point' 
p = Point(3, 4)

#Access the elements using the numeric indices
print(p[0], p[1])

#Access the elements using their names
print(p.x, p.y)
content_copyCOPY

https://www.pynerds.com/python-collections-namedtuple/