What is a NamedTuple in Python?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

NamedTuple is a collection in Python whose data is accessible as key-value pairs like dictionaries and behaves like a tuple by making the data immutable.

Tuple in Python

A tuple is a collection in Python that stores a list of values. The values stored in a tuple are immutable.

Output:

The main problem with using a tuple here is that the values are stored sequentially like a list, but it doesn't tell what the values are. For example, the value 28 in the employee1 tuple can be the employee's id or age. To avoid this ambiguity, we can use a dictionary.

Dictionary in Python

Dictionary is a collection in Python that stores data in the form of a key-value pair. The values stored in a dictionary are mutable.

Output:

Using dictionaries solves the problem of the ambiguity of the data stored but makes the data mutable. NamedTuple can be used to store the data in the form of key-value pairs, also making the data immutable.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Python NamedTuple Syntax

Python's NamedTuple is a part of the collections module. A namedtuple can be created by calling the namedtuple() method with two parameters, typename and field_names.

  • typename - The name of the namedtuple. Example: Student, Employee, etc.
  • field_names - The list of attributes stored in the namedtuple.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Example

Let's create a namedtuple that stores employee details like id, name, age, and salary.

Output:

Explaination:

We created a namedtuple employee with the name Employee that can store the employee details such as id, name, age, and salary. We used the employee instance to store the details of the employees Tony Stark and Steve Rogers.

Operations on NamedTuple

Let's look into the various operation allowed on a namedtuple in Python.

1. Access Operations

Values in a namedtuple can be accessed using index, key, or getattr.

Example:

Output:

2. Conversion Operations

A namedtuple can be converted to and from other data types in Python.

  • _make() - Converts an iterable to a namedtuple.
  • _asdict() - Converts a namedtuple to an OrderedDict
  • ** - Converts a dictionary to a namedtuple.

Example:

Output:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

3. Additional Operations

  • _fields - Returns all the keys of the namedtuple.
  • _replace() - Returns a new namedtuple by replacing the given attributes.

Example:

Output:

Why and When to Use a NamedTuple?

1. Storing Immutable Data

NamedTuple can be used when the data stored in it should be immutable and can be accessed using keys like a dictionary.

Example:

Output:

2. Readability and Maintainability

  • NamedTuple makes the tuple readable and maintainable by defining the names of the data that will be stored in it.

Example:

Explanation:

We created the namedtuple with the list of fields that it stores. The employee named tuple can store id, name, age, and salary. When someone reads this code, it is easy for them to understand the values that are stored in the namedtuple.

3. Easy Conversion

NamedTuple, if required, can be easily converted to a dictionary and vice-versa.

Example:

Output:

Learn More

Please visit the below links to learn more about Python's tuples and dictionaries.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Conclusion

  • NamedTuple is a collection in Python whose data is accessible as key-value pairs like dictionaries and is immutable.
  • NamedTuple behaves like a dictionary and a tuple.
  • The values of a namedtuple can be accessed using an index, key, or getattr() method.
  • NamedTuple can be created from an iterable or a dictionary. Also, a namedtuple can be converted to a dictionary.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more