DEV Community

Paulo GP
Paulo GP

Posted on

Working with Named Tuples in Python

In this post, we'll explore named tuples in Python, using an astronomy theme to provide code examples. Named tuples are a subclass of tuples, which means that they have all the properties of tuples, but with one key difference: their elements can be accessed using dot notation, as well as by index. This makes them a convenient way to store and access structured data. Let's get started.

First, let's create a named tuple to represent a star. We can do this using the namedtuple function from the collections module. This function takes two arguments: the name of the named tuple, and a list of field names:

from collections import namedtuple

Star = namedtuple("Star", ["name", "constellation", "magnitude"])
betelgeuse = Star("Betelgeuse", "Orion", 0.42)
print(betelgeuse)

# Output: Star(name='Betelgeuse', constellation='Orion', magnitude=0.42)
Enter fullscreen mode Exit fullscreen mode

In this example, we create a named tuple called Star with three fields: name, constellation, and magnitude. We then create an instance of the Star named tuple to represent the star Betelgeuse, which is in the constellation Orion and has an apparent magnitude of 0.42.

We can access the elements of a named tuple using dot notation, as well as by index:

print(betelgeuse.name)

# Output: Betelgeuse
Enter fullscreen mode Exit fullscreen mode
print(betelgeuse)

# Output: Betelgeuse
Enter fullscreen mode Exit fullscreen mode

We can also use the _fields attribute to get a list of the field names of a named tuple:

print(Star._fields)

# Output: ('name', 'constellation', 'magnitude')
Enter fullscreen mode Exit fullscreen mode

Named tuples are immutable, just like regular tuples. This means that we cannot change their elements once they are created. However, we can create a new named tuple with modified elements using the _replace method:

rigel = betelgeuse._replace(name="Rigel", magnitude=0.12)
print(rigel)

# Output: Star(name='Rigel', constellation='Orion', magnitude=0.12)
Enter fullscreen mode Exit fullscreen mode

In this example, we create a new named tuple rigel by calling the _replace method on the betelgeuse named tuple. This creates a new named tuple with the same field values as betelgeuse, except for the name and magnitude fields, which we specify as arguments to the _replace method.

We can also convert a named tuple to a dictionary using the _asdict method:

betelgeuse_dict = betelgeuse._asdict()
print(betelgeuse_dict)

# Output: OrderedDict([('name', 'Betelgeuse'), ('constellation', 'Orion'), ('magnitude', 0.42)])
Enter fullscreen mode Exit fullscreen mode

In this example, we call the _asdict method on the betelgeuse named tuple to create an ordered dictionary with the same field names and values as the named tuple.

That concludes our introduction to named tuples in Python. We've covered the basics of creating and accessing named tuples, as well as some of their useful methods and attributes. Named tuples are a convenient way to store and access structured data, and can be a useful tool in your Python toolkit.

Top comments (0)