Python Everyday 02:namedtuple

Today we will see how to enhance code readability by using “namedtuple”.

We use indexing to get items from a tuple as:

However, when the tuple has too many items, it’s hard to remember every item in the tuple, moreover, when you added a new item in the tuple, all index related codes may go wrong. Luckily, we have “namedtuple”, we can name each item and get the value by its name.

The benefits of “namedtuple” are we can build better-understood data structure and decouple index between items in the tuple.