Sling Academy
Home/Python/Working with named tuples in Python (basic and advanced examples)

Working with named tuples in Python (basic and advanced examples)

Last updated: February 12, 2024

Overview

Named tuples in Python are a lightweight, memory-efficient way to create self-documenting, readable object-like instances without the need for writing a full class. Extending the functionality of regular tuples, named tuples assign meaning to each position in a tuple, making your code significantly more readable and maintainable.

Getting Started with Named Tuples

To begin with named tuples, you first need to import them from the collections module. Named tuples are created using the namedtuple() factory function, which generates a new subclass of a tuple with named fields.

from collections import namedtuple

Person = namedtuple('Person', 'name age gender')
john = Person(name='John Doe', age=32, gender='male')

print(john)
print(john.age)

This example creates a Person named tuple with ‘name’, ‘age’, and ‘gender’ fields. It then creates an instance of this named tuple for ‘John Doe’ and demonstrates accessing its fields through dot notation, which is more readable than using integer indexes for tuple elements.

Customizing and Extending Named Tuples

While named tuples are immutable just like regular tuples, you can add methods and default values to named tuples to make them more flexible and powerful. This can be achieved by subclassing the generated named tuple class.

class Employee(namedtuple('EmployeeBase', 'name position salary')):
    __slots__ = () # Prevents the creation of instance dictionaries, making it more memory-efficient

    def __str__(self):
        return f'{self.name}, {self.position}, {self.salary}'

bob = Employee('Bob Smith', 'Developer', 90000)
print(bob)

Here, Employee extends the functionality of a named tuple by adding a custom string representation of the instance. This approach retains the immutability and memory efficiency of named tuples while allowing more complex behaviors.

Advanced Usage: Converting Named Tuples to Dictionaries

One of the more advanced features of named tuples is their ability to be converted into dictionaries. This feature can be incredibly useful for serialization or when interfacing with APIs that expect data in dictionary format.

from collections import namedtuple

SalesRecord = namedtuple('SalesRecord', 'date product quantity')
sale = SalesRecord('2022-07-15', 'Widget', 25)

sale_dict = sale._asdict()
print(sale_dict)

This snippet converts a SalesRecord named tuple into a dictionary using the _asdict() method, allowing for easier manipulation and interaction with parts of Python that leverage dictionaries, like JSON serialization.

Using Named Tuples with Type Annotations

In more advanced Python versions, named tuples can be used in conjunction with type annotations to provide clearer contracts for functions and enhance code readability and maintainability. The typing module’s NamedTuple class allows for explicit type annotations.

from typing import NamedTuple

class Contact(NamedTuple):
    name: str
    email: str = 'N/A' # Default value

contact = Contact('Jane Doe', '[email protected]')
print(contact)
print(contact.email)

This example illustrates using the NamedTuple class from the typing module to define a Contact named tuple with type annotations and default values.

Conclusion

Named tuples in Python offer a compact, efficient way to create structured, readable, and immutable data objects without the overhead of a full class definition. From simple data structures to more complex, extended functionalities, named tuples can significantly enhance code readability and maintainability. The ability to add methods, default values, and type annotations further extends their utility in writing clean, efficient Python code.

Next Article: How to compare 2 tuples in Python (basic and advanced)

Previous Article: Python: Checking if 2 ranges overlap (4 examples)

Series: Working with Dict, Set, and Tuple in Python

Python

You May Also Like

  • Python Warning: Secure coding is not enabled for restorable state
  • Python TypeError: write() argument must be str, not bytes
  • 4 ways to install Python modules on Windows without admin rights
  • Python TypeError: object of type ‘NoneType’ has no len()
  • Python: How to access command-line arguments (3 approaches)
  • Understanding ‘Never’ type in Python 3.11+ (5 examples)
  • Python: 3 Ways to Retrieve City/Country from IP Address
  • Using Type Aliases in Python: A Practical Guide (with Examples)
  • Python: Defining distinct types using NewType class
  • Using Optional Type in Python (explained with examples)
  • Python: How to Override Methods in Classes
  • Python: Define Generic Types for Lists of Nested Dictionaries
  • Python: Defining type for a list that can contain both numbers and strings
  • Using TypeGuard in Python (Python 3.10+)
  • Python: Using ‘NoReturn’ type with functions
  • Type Casting in Python: The Ultimate Guide (with Examples)
  • Python: Using type hints with class methods and properties
  • Python: Typing a function with default parameters
  • Python: Typing a function that can return multiple types