namedtuple helpers

EasyCollections Programs · 14 of 20

Write a Python program using _asdict, _replace and _make.

Advertisement
Output
Point(x=3, y=4) {'x': 3, 'y': 4} Point(x=10, y=4)

Approach

  1. These helpers convert and copy named tuples.

Solution

Python
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.

More collections programs

← All 20 Collections Programs

Leave a Comment