![]() |
VOOZH | about |
Python supports a type of container dictionary called "namedtuple()" present in the module "collections". In this article, we are going to see how to Create a NameTuple and operations on NamedTuple.
In Python, NamedTuple is present inside the collections module. It provides a way to create simple, lightweight data structures similar to a class, but without the overhead of defining a full class. Like dictionaries, they contain keys that are hashed to a particular value. On the contrary, it supports both access from key-value and iteration, the functionality that dictionaries lack.
namedtuple(typename, field_names)
- typename - The name of the namedtuple.
- field_names - The list of attributes stored in the namedtuple.
Example: Code implementation of NamedTuple is shown in Python.
The Student age using index is : 19 The Student name using keyname is : Nandini
Below are the following operations that can done by using namedtuple():
This creates a new namedtuple class using the namedtuple() function from the collections module. The first argument is the name of the new class, and the second argument is a list of field names.
1 2
Namedtuples in Python provide convenient ways to access their fields. Below are some access operations provided in Python for NamedTuple:
The attribute values of namedtuple() are ordered and can be accessed using the index number unlike dictionaries which are not accessible by index. In this example, we are accessing the student's by using index.
The Student age using index is : 19
Access by keyname is also allowed as in dictionaries. In this example, we are using keyname to access the student's name.
The Student name using keyname is : Nandini
This is yet another way to access the value by giving namedtuple and key value as its argument. In this example, we are using getattr() to access the student's id in the given namedtuple.
The Student DOB using getattr() is : 2541997
Namedtuples provide a few useful conversion operations to work with other data types in Python. Below are the following conversion operations that is provided for namedtuples in Python:
This function is used to return a namedtuple() from the iterable passed as argument. In this example, we are using _make() to convert the list "li" into namedtuple.
The namedtuple instance using iterable is : Student(name='Manjeet', age='19', DOB='411997')
This function returns the OrderedDict() as constructed from the mapped values of namedtuple(). In this example, we are using _asdict() to convert the input list into namedtuple instance.
The OrderedDict instance using namedtuple is :
OrderedDict([('name', 'Nandini'), ('age', '19'), ('DOB', '2541997')])
This function is used to convert a dictionary into the namedtuple(). In this example, we are using "**" to convert the input list into namedtuple.
The namedtuple instance from dict is : Student(name='Nikhil', age=19, DOB='1391997')
There are some additional operations that are provided in Python for NamedTuples:
This data attribute is used to get all the keynames of the namespace declared. In this example, we are using _fields to get all the keynames of the namespace declared.
All the fields of students are :
('name', 'age', 'DOB')
_replace() is like str.replace() but targets named fields( does not modify the original values). In this example, we are using _replace() to replace a name from "Nandini" to "Manjeet".
returns a new namedtuple : Student(name='Manjeet', age='19', DOB='2541997')
This function returns a new instance of the Student class, by taking the values that we want to assign to the keys in the named tuple. In this example, we are using __new__() to return a new instance of the Student class.
Student(name='Himesh', age='19', DOB='26082003')
This function returns the named tuple as a plain tuple. In this example, we are doing the same by using __getnewargs__().
('Himesh', '19', '26082003')
- Mutability: Instances of a class can be mutable or immutable, while
namedtupleinstances are immutable.- Methods: Classes can contain methods (functions), while
namedtupleprimarily provides a way to store data with named fields.- Inheritance: Classes support inheritance, allowing the creation of complex hierarchies, whereas
namedtupledoes not support inheritance.
- Type Checking:
TypedDict(from thetypingmodule) provides type hints for dictionaries with specific key-value pairs, useful for type checking.namedtupledoes not provide type hints.- Mutability:
TypedDictinstances are mutable, allowing changes to the values, whilenamedtupleinstances are immutable.- Structure:
TypedDictis used to define the structure of dictionaries with specific types for each key, whereasnamedtupleprovides named fields for tuple-like data.