Convert a namedtuple into a dictionary
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Python's namedtuple has a built-in _asdict() method that converts it to an OrderedDict (Python 3.7 and earlier) or a regular dict (Python 3.8+). For nested namedtuples, you need recursive conversion. The dataclasses.asdict() function handles this automatically for dataclasses, which are the modern alternative to namedtuples for structured data.
Basic Conversion with _asdict()
The _asdict() method is part of the namedtuple API (the underscore prefix does not mean it is private — namedtuples use this convention to avoid name conflicts with field names).
Using dict() Constructor
Nested Namedtuples
_asdict() only converts the top level — nested namedtuples remain as namedtuples:
Converting Back: Dict to Namedtuple
JSON Serialization
Modern Alternative: dataclasses
typing.NamedTuple (Python 3.6+)
Performance Comparison
_asdict() is faster than dataclasses.asdict() because it does less work (no recursive copying). Manual dict construction is fastest of all.
Common Pitfalls
- Expecting _asdict() to handle nested namedtuples:
_asdict()only converts the top-level namedtuple to a dict. Nested namedtuples remain as namedtuple objects. Write a recursive conversion function or switch todataclasses.asdict()which handles nesting automatically. - Treating _asdict as private: The underscore prefix in
_asdict,_fields, and_replaceis part of the namedtuple API, not a privacy indicator. These methods are the documented, correct way to interact with namedtuples. They use underscores to avoid conflicts with user-defined field names. - Modifying the returned dict and expecting the namedtuple to change: Namedtuples are immutable. The dict from
_asdict()is a copy. Modifyingd["x"] = 99does not change the original namedtuple. Use_replace(x=99)to create a new namedtuple with modified values. - Using dict(namedtuple) directly:
dict(p)does not work on namedtuples — it raisesTypeErrorbecause namedtuples are not mappings. You must usep._asdict()ordict(zip(p._fields, p)). Dataclasses also requireasdict(dc), notdict(dc). - Using namedtuple when you need mutability: Namedtuples are immutable — you cannot change field values after creation. If you need mutable records that convert to dicts, use
@dataclasswhich supports bothasdict()and attribute assignment.
Summary
- Use
namedtuple._asdict()to convert a namedtuple to a dictionary _asdict()returns a regulardictin Python 3.8+ (OrderedDict in earlier versions)- For nested namedtuples, write a recursive converter or use
dataclasses.asdict() - Convert dict back to namedtuple with
MyTuple(**my_dict) typing.NamedTupleprovides the same_asdict()with added type annotations- Prefer
dataclassesovernamedtuplefor new code — they handle nesting, mutability, and conversion better
Related reading
- Convert a Pandas DataFrame to a dictionary
- Convert a PHP object to an associative array
- Convert a python dict to a string and back
- Convert a String representation of a Dictionary to a dictionary
- Convert a python 'type' object to a string
- Convert a String representation of a Dictionary to a dictionary
- Convert a string representation of a hex dump to a byte array using Java?
- Convert all strings in a list to integers

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.