When working with tuples in Python, you can store multiple values and access them using indexes. However, if a tuple contains many fields, remembering field ordering and accessing them using index becomes quite a task and it is also less readable. This is where Named Tuple in Python shines, offering a cleaner, more descriptive way to access tuple elements.
What is a Named Tuple in Python
A named tuple is a custom tuple data type which provides ability to refer the items in the tuple by both item names as well as by index position, for example, my_tuple.field_name instead of just integer indices like my_tuple[0].
Key features are:
- Provides the same performance as regular tuples.
- Maintains immutability, values cannot be changed once created.
- Improves readability by allowing field name access.
Creating a named tuple
Named tuples are created using the namedtuple() factory function from the collections module.
from collections import namedtuple #Syntax for creating named tuple namedtuple(typename, field_names)
- typename- The name of the new tuple subclass.
- field_names- Represent fields which are stored in the named tuple. They can be provided as a sequence of strings like ['field1', 'field2'] or as a single space/comma‑separated string representing field names, for example 'field1 field2' or 'field1', 'field2'.
For example-
Employee = namedtuple('Employee', ['id', 'name','age'])
Here named tuple Employee is created which can store field id, name and age.
You can also provide fieldnames as a single string separated by white space.
Employee = namedtuple('Employee', 'id name age')
Python named tuple example
from collections import namedtuple
# Declaring namedtuple()
Employee = namedtuple('Employee', 'id name age')
# Create custom tuple types
e1 = Employee('123', 'Joe', '28')
e2 = Employee('124', 'Lisa', '31')
# Access using index
print('Employee Age', e1[2])
# Access using field name
print('Employee Name', e2.name)
#iterating
for emp in [e1, e2]:
print('Employee Id', emp.id, 'Employee Name', emp.name, 'Employee Age', emp.age)
Output
Employee Age 28 Employee Name Lisa Employee Id 123 Employee Name Joe Employee Age 28 Employee Id 124 Employee Name Lisa Employee Age 31
Use Cases of Named Tuple in Python
1. One of the most common scenarios where Named Tuple in Python proves useful is when you need to store multiple objects of the same type. Instead of defining a full class with attributes and then creating instances, you can use a namedtuple, which is lightweight, easier to create, and still provides clear field names for readability.
Here is the example to store multiple Employees in a list using named tuples.
from collections import namedtuple
# Declaring namedtuple()
Employee = namedtuple('Employee', 'id name age')
#list
employees = []
# storing named tuples
employees.append(Employee('123', 'Joe', '28'))
employees.append(Employee('124', 'Lisa', '31'))
# Access using index
print('Employee Age', employees[0][2])
# Access using field name
print('Employee Name', employees[0].name)
# iterate list
for emp in employees:
print('Employee Id', emp.id, 'Employee Name', emp.name, 'Employee Age', emp.age)
Output
Employee Age 28 Employee Name Joe Employee Id 123 Employee Name Joe Employee Age 28 Employee Id 124 Employee Name Lisa Employee Age 31
2. Another use case of Named Tuple in Python is when working with structured data such as CSV files. Instead of manually handling indexes or writing a full class to represent each record, you can map the CSV fields directly into a namedtuple, making the code more readable and maintainable.
import csv
from collections import namedtuple
# Declaring namedtuple()
Employee = namedtuple('Employee', 'id name age')
for emp in map(Employee._make, csv.reader(open("F:\\NETJS\\employee.csv", "r"))):
print(emp.id, emp.name, emp.age)
Here note that _make method of the named tuple is used which is a class method that makes a new instance from an existing sequence or iterable.
Methods in named tuple
In addition to the methods inherited from tuples, named tuples support three additional methods.
1. _make(iterable)- The _make() class method creates a new namedtuple instance from an existing sequence or iterable. This is especially useful when you already have data in a list or tuple and want to convert it into a namedtuple. We have already seen an example of using _make().
2. _asdict()- Returns the namedtuple instance as a new dict (regular dict Python 3.12 onward, before that it was OrderedDict) which maps field names to their corresponding values.
from collections import namedtuple
# Declaring namedtuple()
Employee = namedtuple('Employee', 'id name age')
# Create custom tuple types
e1 = Employee('123', 'Joe', '28')
print(e1._asdict())
Output
{'id': '123', 'name': 'Joe', 'age': '28'}
3. _replace(**kwargs)- Returns a new instance of the named tuple replacing specified fields with new values.
from collections import namedtuple
# Declaring namedtuple()
Employee = namedtuple('Employee', 'id name age')
# Create custom tuple types
e1 = Employee('123', 'Joe', 28)
print(e1)
print(e1._replace(age=30))
Output
Employee(id='123', name='Joe', age=28) Employee(id='123', name='Joe', age=30)
That's all for this topic Named Tuple in Python. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Python Tutorial Page
Related Topics
You may also like-



