Python Practice › Collections Programs › Program 14
namedtuple helpers
Write a Python program using _asdict, _replace and _make.
Output
Point(x=3, y=4) {'x': 3, 'y': 4} Point(x=10, y=4)Approach
- These helpers convert and copy named tuples.
Solution
from collections import namedtuple Point = namedtuple("Point", "x y")p = Point._make([3, 4])print(p, p._asdict(), p._replace(x=10))Output
Point(x=3, y=4) {'x': 3, 'y': 4} Point(x=10, y=4)Learn the concept
New to this topic? Read the lesson, then come back.