
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
NamedTuple in Python
The NamedTuple is another class, under the collections module. Like the dictionary type objects, it contains keys and that are mapped to some values. In this case we can access the elements using keys and indexes.
To use it at first we need to import it the collections standard library module.
import collections
In this section we will see some functions of the NamedTuple class.
The accessing methods of NamedTuple
From NamedTuple, we can access the values using indexes, keys and the getattr() method. The attribute values of NamedTuple are ordered. So we can access them using the indexes.
The NamedTuple converts the field names as attributes. So using getattr() it is possible to get the data from that attribute.
Example Code
import collections as col #create employee NamedTuple Employee = col.namedtuple('Employee', ['name', 'city', 'salary']) #Add two employees e1 = Employee('Asim', 'Delhi', '25000') e2 = Employee('Bibhas', 'Kolkata', '30000') #Access the elements using index print('The name and salary of e1: ' + e1[0] + ' and ' + e1[2]) #Access the elements using attribute name print('The name and salary of e2: ' + e2.name + ' and ' + e2.salary) #Access the elements using getattr() print('The City of e1 and e2: ' + getattr(e1, 'city') + ' and ' + getattr(e2, 'city'))
Output
The name and salary of e1: Asim and 25000 The name and salary of e2: Bibhas and 30000 The City of e1 and e2: Delhi and Kolkata
Conversion procedures of NamedTuple
There are some methods to convert other collections to NamedTuple. The _make() method can be used to convert an iterable object like list, tuple, etc to NamedTuple object.
We can also convert a dictionary type object to NamedTuple object. For this conversion, we need the ** operator.
NamedTuple can return the values with keys as OrderedDict type object. To make it OrderedDict, we have to use the _asdict() method.
Example Code
import collections as col #create employee NamedTuple Employee = col.namedtuple('Employee', ['name', 'city', 'salary']) #List of values to Employee my_list = ['Asim', 'Delhi', '25000'] e1 = Employee._make(my_list) print(e1) #Dict to convert Employee my_dict = {'name':'Bibhas', 'city' : 'Kolkata', 'salary' : '30000'} e2 = Employee(**my_dict) print(e2) #Show the named tuple as dictionary emp_dict = e1._asdict() print(emp_dict)
Output
Employee(name='Asim', city='Delhi', salary='25000') Employee(name='Bibhas', city='Kolkata', salary='30000') OrderedDict([('name', 'Asim'), ('city', 'Delhi'), ('salary', '25000')])
Some additional operations on NamedTuple
There are some other method like _fields() and _replace(). 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.
Example Code
import collections as col #create employee NamedTuple Employee = col.namedtuple('Employee', ['name', 'city', 'salary']) #Add an employees e1 = Employee('Asim', 'Delhi', '25000') print(e1) print('The fields of Employee: ' + str(e1._fields)) #replace the city of employee e1 e1 = e1._replace(city='Mumbai') print(e1)
Output
Employee(name='Asim', city='Delhi', salary='25000') The fields of Employee: ('name', 'city', 'salary') Employee(name='Asim', city='Mumbai', salary='25000')