
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I convert a Python Named tuple to a dictionary?
Namedtuple class is defined in the collections module. It returns a new tuple subclass. The new subclass is used to create tuple-like objects that have fields accessible by attribute lookup as well as being indexable and iterable. The constructor takes type name and field list as arguments. For example, a student namedtuple is declared as follows −
>>> from collections import namedtuple >>> student=namedtuple("student","name, age, marks")
The object of this namedtuple class is declared as −
>>> s1=student("Raam",21,45)
This class has _asdict() method which returns orderdict() object
>>> d=s1._asdict() >>> d OrderedDict([('name', 'Raam'), ('age', 21), ('marks', 45)])
To obtain regular dictionary object use dict() function
>>> dct=dict(d) >>> dct {'name': 'Raam', 'age': 21, 'marks': 45}
Good
- Related Questions & Answers
- How I can convert a Python Tuple into Dictionary?
- How can I convert a Python tuple to string?
- How can I convert a Python tuple to an Array?
- How can I convert Python tuple to C array?
- How can I convert Python strings into tuple?
- How can I convert Python dictionary to JavaScript hash table?
- How can I append a tuple into another tuple in Python?
- How we can create a dictionary from a given tuple in Python?
- Convert tuple to adjacent pair dictionary in Python
- How can I create a non-literal python tuple?
- How can I subtract tuple of tuples from a tuple in Python?
- How can I convert bytes to a Python string?
- Convert Nested Tuple to Custom Key Dictionary in Python
- How I can dump a Python tuple in JSON format?
- How can I create a Python tuple of Unicode strings?
Advertisements