NamedTuple in Python

NamedTuple in Python

The NamedTuple is one of the classes under the collections module. It contains keys which are mapped to some values, just like dictionary-type objects. In this case, the user can access these elements with the help of keys and indexes.

To use it, at first, the user has to import the collections standard library module.

For example:

In this article, we will discuss some functions of the NamedTuple class.

The accessing Functions of NamedTuple

By using NamedTuple class, the users can access the values with the help of index, keys, and the getattr() function. The attribute values of the NamedTuple class are in order so that the users can access the values through the indexes.

The NamedTuple class will convert the field names as attributes so that the user can use the getattr() function for getting the data from these attributes.

Example:

Output:

The name and salary of employee1: Jack and Goa
The name and salary of employee2: Ross and Kolkata
The name and city of employee3: Joey and Kerela
The name and city of employee4: John and Jammu
The City of employee1 and employee2: Goa and 35000
The City of employee3 and employee4: Kerela and 40000

Conversion Procedure of the NamedTuple Class

There are few methods used for converting other collections into NamedTuple. The users can use the _make_() function for converting the iterable object, such as list, tuple, etc., into NamedTuple class object.

The users can also convert the dictionary type object into a NamedTuple class object. For converting the dictionary type into NamedTuple type, users have to use the ** operator.

The NamedTuple can return the values with keys as the OrderedDict type object. For converting it into OrderedDict, the user has to use the _asdict() function.

Example:

Output:

Employee(name='Jack', city='Goa', salary='25000')
Employee(name='Ross', city='Kolkata', salary='35000')
Employee(name='Joey', city='Kerala', salary='55000')
Employee(name='John', city='Jammu', salary='40000')
OrderedDict([('name', 'Jack'), ('city', 'Goa'), ('salary', '25000')])
OrderedDict([('name', 'Ross'), ('city', 'Kolkata'), ('salary', '35000')])
OrderedDict([('name', 'Joey'), ('city', 'Kerala'), ('salary', '55000')])
OrderedDict([('name', 'John'), ('city', 'Jammu'), ('salary', '40000')])

Some Additional Operation of NamedTuple

There are some other functions like _fields() and _replace() function. the users can use the _fields() function for checking the different fields of NamedTuple class. The _replace() function is used for replacing the value of attributes.

Example:

Output:

Employee(name='Jack', city='Goa', salary='25000')
Employee(name='Ross', city='Kolkata', salary='35000')
The fields of Employee1: ('name', 'city', 'salary')
The fields of Employee2: ('name', 'city', 'salary')
Employee(name='Jack', city='New Dehli', salary='25000')
Employee(name='Ross', city='Assam', salary='35000')

Conclusion

In this article, we have discussed what NamedTuple is and how the user can access its different functions and operations.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263519.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论