Answers for "named tuple example"

2

create a named tuple python

>>> from collections import namedtuple
>>> Person = namedtuple('Person', 'first_name last_name zip_code')
>>> p1 = Person('Joe', 'Schmoe', '93002')
>>> p1.first_name
'Joe'
>>> p1[0]
'Joe'
>>> p1.zip_code
'92002'
>>> len(p1)
3
>>> type(p1)
<class '__main__.Person'>
>>>
Posted by: Guest on April-21-2021

Python Answers by Framework

Browse Popular Code Answers by Language