How to print a tuple with named fields (namedtuple) as a dictionary in Python

1 Answer

0 votes
import collections

Worker = collections.namedtuple('Worker', 'name age profession')

Tom = Worker(name='Tom', age=45, profession='Programmer')

print(Tom._asdict())    

    

'''
run:

OrderedDict([('name', 'Tom'), ('age', 45), ('profession', 'Programmer')])

'''

 



answered May 11, 2019 by avibootz

Related questions

2 answers 232 views
2 answers 238 views
2 answers 338 views
2 answers 122 views
...