NamedTuple is a factory function available in collections. They are quite similar to the dictionary function that we had learned in earlier articles. But, it is better than a dictionary because it supports access from key-value and iterations.


from collections import namedtuple details = namedtuple('Student', ['name', 'age', 'DOB']) D = details('Harshit', '20', 2061819) print("The Student age using index is given - ", end="") print(D[1]) print("The Student name using keyname is given - ", end="") print(D.name)
Output –
The Student age using index is given - 20 The Student name using keyname is given – Harshit
Operations on NamedTuple
Now, we will see some of the operations on NamedTuple –
Access by Index: This feature was not available in the dictionary but here, you can access attribute values using the index number in NamedTuple.
Access by keyname: We can access a particular attribute value using the keyname. This is quite similar to we have learned in Dictionary.
Using getattr(): This is also a way to access through namedTuple and key value.
We will now see an example of this –
import collections Details = collections.namedtuple('Student', ['name', 'age', 'DOB']) D = Details('Harshit', '20', '2061819') print("The Student age using index is given : ", end="") print(D[1]) print("The Student name using keyname is given : ", end="") print(D.name) print("The Student DOB using getattr() is given : ", end="") print(getattr(D, 'DOB'))
Output –
The Student age using index is given : 20 The Student name using keyname is : Harshit The Student DOB using getattr() is : 20011687
Conversion Procedures of Named Tuple –
Python provides some methods to convert other collections to NamedTuple.
The _make() method can be used to convert an iterable object like a list, tuple, etc to a NamedTuple object.
We can also convert a dictionary-type object to a NamedTuple object. For this conversion, we need the ** operator.
The NamedTuple can return the values with keys as an OrderedDict type object. To make it OrderedDict, we have to use the _asdict() method.
We will understand this with the help of the example given below –
import collections as col Employee = col.namedtuple('Employee', ['name', 'city', 'salary']) my_list = ['Harshit', 'Mumbai', '25000'] e1 = Employee._make(my_list) print(e1) my_dict = {'name':'Johnny', 'city' : 'Kolkata', 'salary' : '90000'} e2 = Employee(**my_dict) print(e2) emp_dict = e1._asdict() print(emp_dict)
Output –
Employee(name='Harshit', city='Mumbai', salary='25000') Employee(name='Johnny', city='Kolkata', salary='90000') OrderedDict([('name', 'Harshit'), ('city', 'Mumbai'), ('salary', '25000')])
Apart from these, python also provides some other functions for NamedTuples. For example, Using the _fields() method we can check what are the different fields of NamedTuple. The _replace() method is used to replace the value of some other value.
import collections as col Employee = col.namedtuple('Employee', ['name', 'city', 'salary']) e1 = Employee('Harshit', 'Mumbai', '35000') print(e1) print('The fields of Employee: ' + str(e1._fields)) e1 = e1._replace(city='Kolkata') print(e1)
Output –
Employee(name='Harshit', city='Mumbai', salary='35000') The fields of Employee: ('name', 'city', 'salary') Employee(name='Harshit', city=’Kolkata', salary='25000')
So, in this article, we learned about NamedTuple and its functions in Python. While programming, we will come across situations where we cannot use the Dictionary and we have to use this NamedTuple.