English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Voice Recognition in Python using Google Speech API

NamedTuple is another class under the collections module. Like dictionary type objects, it contains keys and maps to certain values. In this case, we can use keys and indices to access elements.

Firstly, we need to import it from the standard library module of collections.

import collections

In this section, we will see some features of the NamedTuple class.

NamedTuple access methods

In NamedTuple, we can use index, key, andgetattr()methods to access values. The attribute values of NamedTuple are ordered. This allows us to access them using an index.

NamedTuple converts field names to properties. Therefore, usegetattr()Data can be obtained from this 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 Result

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

NamedTuple conversion process

There are some methods to convert other collections to NamedTuple. The _make() method can be used to convert iterable objects (such as lists, tuples, etc.) to NamedTuple objects.

We can also convert dictionary type objects to NamedTuple objects. For this conversion, we need**operator.

NamedTuple can be used as a key to return an OrderedDict type object. To make it an OrderedDict, we must 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 Result

Employee(name='Asim', city='Delhi', salary='25000')
Employee(name='Bibhas', city='Kolkata', salary='30000')
OrderedDict([('name', 'Asim'), ('city', 'Delhi'), ('salary', '25000')])

Some other operations on NamedTuple

There are some other methods, such as _fields() and _replace(). The _fields() method allows us to check the different fields of the NamedTuple. The _replace() method is used to replace some other values.

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 Result

Employee(name='Asim', city='Delhi', salary='25000')
The fields of Employee: ('name', 'city', 'salary')
Employee(name='Asim', city='Mumbai', salary='25000')