Namedtuple in Python
Python supports a type of container dictionary called “namedtuple()” present in the module “collections“. In this article, we are going to review how to Create a NameTuple and operations enabled NamedTuple.
What is NamedTuple in Python?
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. similar as 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.
Python NamedTuple Syntax
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.
# Python code to demonstrate namedtuple()
from collections import namedtuple
# Declaring namedtuple()
Student = namedtuple('Student', ['name', 'age', 'DOB'])
# Adding values
S = Student('Nandini', '19', '2541997')
# Access using index
print("The Student age using index is : ", end="")
print(S[1])
# Access using name
print("The Student name using keyname is : ", end="")
print(S.name)
The Student age using index is : 19 The Student name using keyname is : Nandini
Operations enabled NamedTuple
Below are the following operations that can done by using namedtuple():
- Create a NameTuple
- Access Operations
- Conversion Operations
- Additional Operations
Create a NameTuple in Python
This creates a new namedtuple class using the namedtuple() function/method from the collections module. The first argument is the name of the new class, and the second argument is a list of field names.
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(x=1, y=2)
print(p.x, p.y)
1 2
Access Operations
Namedtuples in Python provide convenient ways to access their fields. Below are some access operations provided in Python for NamedTuple:
- Access by index
- Access by keyname
- Access Using getattr()
Access By Index
The property 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.
# importing "collections" for namedtuple()
import collections
# Declaring namedtuple()
Student = collections.namedtuple('Student', ['name', 'age', 'DOB'])
# Adding values
S = Student('Nandini', '19', '2541997')
# Access using index
print("The Student age using index is : ", end="")
print(S[1])
The Student age using index is : 19
Access by keyname
Access by keyname is also allowed when in dictionaries. In this example, we are using keyname to access the student’s name.
# importing "collections" for namedtuple()
import collections
# Declaring namedtuple()
Student = collections.namedtuple('Student', ['name', 'age', 'DOB'])
# Adding values
S = Student('Nandini', '19', '2541997')
# Access using name
print("The Student name using keyname is : ", end="")
print(S.name)
The Student name using keyname is : Nandini
Access Using getattr()
This is yet another way to access the value by giving namedtuple and key value when its argument. In this example, we are using getattr() to access the student’s id in the given namedtuple.
# importing "collections" for namedtuple()
import collections
# Declaring namedtuple()
Student = collections.namedtuple('Student', ['name', 'age', 'DOB'])
# Adding values
S = Student('Nandini', '19', '2541997')
# Access using getattr()
print("The Student DOB using getattr() is : ", end="")
print(getattr(S, 'DOB'))
The Student DOB using getattr() is : 2541997
Conversion Operations
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:
- Using _make()
- Using _asdict()
- Using **(double star) operator
Conversion Using _make()
This function/method is have used to return a namedtuple() from the iterable passed when argument. In this example, we are using _make() to convert the list “li” into namedtuple.
# importing "collections" for namedtuple()
import collections
# Declaring namedtuple()
Student = collections.namedtuple('Student',
['name', 'age', 'DOB'])
# Adding values
S = Student('Nandini', '19', '2541997')
# initializing iterable
li = ['Manjeet', '19', '411997']
di = {'name': "Nikhil", 'age': 19, 'DOB': '1391997'}
# using _make() to return namedtuple()
print("The namedtuple instance using iterable is : ")
print(Student._make(li))
The namedtuple instance using iterable is : Student(name='Manjeet', age='19', DOB='411997')
Conversion Operation Using _asdict()
This function/method returns the OrderedDict() when constructed from the mapped values of namedtuple(). In this example, we are using _asdict() to convert the input list into namedtuple instance.
import collections
# Declaring namedtuple()
Student = collections.namedtuple('Student',
['name', 'age', 'DOB'])
# Adding values
S = Student('Nandini', '19', '2541997')
# initializing iterable
li = ['Manjeet', '19', '411997']
# initializing dict
di = {'name': "Nikhil", 'age': 19, 'DOB': '1391997'}
# using _asdict() to return an OrderedDict()
print("The OrderedDict instance using namedtuple is : ")
print(S._asdict())
The OrderedDict instance using namedtuple is : OrderedDict([('name', 'Nandini'), ('age', '19'), ('DOB', '2541997')])
Using “**” (double star) operator
This function/method is have used to convert a dictionary into the namedtuple(). In this example, we are using “**” to convert the input list into namedtuple.
import collections
# Declaring namedtuple()
Student = collections.namedtuple('Student',
['name', 'age', 'DOB'])
# Adding values
S = Student('Nandini', '19', '2541997')
# initializing iterable
li = ['Manjeet', '19', '411997']
# initializing dict
di = {'name': "Nikhil", 'age': 19, 'DOB': '1391997'}
# using ** operator to return namedtuple from dictionary
print("The namedtuple instance from dict is : ")
print(Student(**di))
The namedtuple instance from dict is : Student(name='Nikhil', age=19, DOB='1391997')
Additional Operations
There are some additional operations that are provided in Python for NamedTuples:
- _fields
- _replace()
- __new__()
- __getnewargs__()
_fields
This data property is have 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.
import collections
Student = collections.namedtuple('Student', ['name', 'age', 'DOB'])
# Adding values
S = Student('Nandini', '19', '2541997')
# using _fields to display all the keynames of namedtuple()
print("All the fields of students are : ")
print(S._fields)
All the fields of students are: ('name', 'age', 'DOB')
_replace()
_replace() is simillar as 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”.
import collections
# Declaring namedtuple()
Student = collections.namedtuple('Student',
['name', 'age', 'DOB'])
# Adding values
S = Student('Nandini', '19', '2541997')
# ._replace returns a new namedtuple,
# it does not modify the original
print("returns a new namedtuple : ")
print(S._replace(name='Manjeet'))
returns a new namedtuple : Student(name='Manjeet', age='19', DOB='2541997')
__new__()
This function/method returns a new instance of the Student class, by taking the values that we may be interested 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.
import collections
# Declaring namedtuple()
Student = collections.namedtuple('Student', ['name', 'age', 'DOB'])
# Adding values
S = Student('Nandini', '19', '2541997')
# Student.__new__ returns a new instance of Student(name,age,DOB)
print(Student.__new__(Student,'Himesh','19','26082003'))
Student(name='Himesh', age='19', DOB='26082003')
__getnewargs__()
This function/method returns the named tuple when a plain tuple. In this example, we are doing the same by using __getnewargs__().
import collections
# Declaring namedtuple()
Student = collections.namedtuple('Student', ['name', 'age', 'DOB'])
# Adding values
S = Student('Nandini', '19', '2541997')
H=Student('Himesh','19','26082003')
# .__getnewargs__ returns the named tuple when a plain tuple
print(H.__getnewargs__())
('Himesh', '19', '26082003')
Namedtuple in Python – FAQs
What is a namedtuple in Python?
A
namedtuple
in Python is a subclass of tuples that allows you to access its elements by name when well when by index. It is part of thecollections
module and provides a way to create self-documenting, immutable data structures.Example:
from collections import namedtuple Point = namedtuple('Point', ['x', 'y']) p = Point(1, 2) print(p.x, p.y) # Output: 1 2
What is the difference between tuple and namedtuple?
- Access by Name: In a
namedtuple
, elements can be accessed using property names, when in a regular tuple, elements are accessed by index.- Readability:
namedtuple
provides better readability and self-documentation because you can use names to describe the data.- Immutability: Both
namedtuple
and tuple are immutable, butnamedtuple
offers named fields.
What is a tuple for Python?
A tuple in Python is an immutable, ordered collection of elements. It is defined by enclosing elements in parentheses and separating them with commas.
t = (1, 2, 3) print(t) # Output: (1, 2, 3)
What is the difference between class and namedtuple?
- Mutability: Instances of a class can be mutable or immutable, when
namedtuple
instances are immutable.- Methods: Classes can contain functions/methods (functions), when
namedtuple
primarily provides a way to store data with named fields.- Inheritance: Classes support inheritance, allowing the creation of complex hierarchies, whereas
namedtuple
does not support inheritance.
What is the difference between typed dict and namedtuple?
- Type Checking:
TypedDict
(from thetyping
module) provides type hints for dictionaries with specific key-value pairs, useful for type checking.namedtuple
does not provide type hints.- Mutability:
TypedDict
instances are mutable, allowing changes to the values, whennamedtuple
instances are immutable.- Structure:
TypedDict
is have used to define the structure of dictionaries with specific types for each key, whereasnamedtuple
provides named fields for tuple-like data.