A downside of plain
tuples is that the data you store in them can only be pulled out by
accessing it through integer indexes.
You can’t give names to individual properties stored in a
This can impact code readability.
tuple is always an ad-hoc structure.
It’s hard to ensure that two
tuples have the same number of fields and the same
properties stored on them.
This makes it easy to introduce “slip-of-the-mind” bugs by mixing up the field order.
namedtuples aim to solve these two problems.
namedtuples are immutable just like regular
Once you store something in them you can’t modify it.
Each object stored in a
namedtuple can be accessed through a unique (human-readable)
This frees you from having to remember integer indexes,
or resorting to workarounds like defining integer constants as mnemonics for your indexes.
You can still access the values by their index.
namedtuples can be used as a drop-in replacement for regular
How to create a
from collections import namedtuple Car = namedtuple('Car' , 'color mileage')
You can also pass a list with string field names. The advantage of using a proper list is that it’s easier to reformat this code if you need to split it across multiple lines:
Car = namedtuple('Car', [ ... 'color', ... 'mileage', ... ])
You’ll even get a nice string representation for free, which saves some typing and redundancy.
When it comes to memory usage
namedtuples are also “better” than regular classes
and just as memory efficient as regular
._fields property gives you a list of the constituent field names.
_asdict() helper returns the contents of a
namedtuple as a dictionary.
This is great for avoiding typos when generating JSON-output, for example.
_replace() function creates a (shallow) copy of a
and allows you to selectively replace some of its fields.
_make() classmethod can be used to create new instances of a
namedtuple from a
sequence or iterable.