All Courses

What is a named tuple in Python, and how is it different from a regular tuple or a dictionary?

By, a month ago
  • Bookmark

In Python, what do "named tuples" refer to?

Named tuple
1 Answer

A named tuple in Python is a subclass of a regular tuple that has fields with names assigned to them. It is similar to a regular tuple in that it is immutable, meaning that it cannot be modified once it is created. However, it differs from a regular tuple in that the fields can be accessed by name instead of index.

On the other hand, a dictionary is a mutable data structure that stores key-value pairs. It allows you to access its values by keys, which can be any hashable data type. Unlike a tuple, a dictionary is unordered, and its keys are unique.

In summary, named tuples are useful when you want to represent a simple object with a fixed set of attributes. They are more memory-efficient than dictionaries and provide better readability and semantics compared to regular tuples.

Your Answer


Live Masterclass on : "How Machine Get Trained in Machine Learning?"

Mar 30th (7:00 PM) 516 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More