What is a NamedTuple in Python?
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.
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.
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:
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.
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.