Created
August 2, 2021 06:57
-
-
Save mwrites/b3d661e987528c44d8de364084243016 to your computer and use it in GitHub Desktop.
Python convert dictionary to namedtuple
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import namedtuple | |
Parts = {'id_num':'1234', 'desc':'Ford Engine', | |
'cost':1200.00, 'amount':10} | |
parts = namedtuple('Parts', Parts.keys())(**Parts) | |
print (parts) | |
#Parts(amount=10, cost=1200.0, id_num='1234', desc='Ford Engine') | |
# OR | |
parts = namedtuple('Parts', Parts.keys()) | |
print (parts) | |
#<class '__main__.Parts'> | |
auto_parts = parts(**Parts) | |
print (auto_parts) | |
#Parts(amount=10, cost=1200.0, id_num='1234', desc='Ford Engine') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment