Home Artificial Intelligence Python “Tuple+”: Named Tuples

Python “Tuple+”: Named Tuples

0
Python “Tuple+”: Named Tuples

PYTHON PROGRAMMING

Tuples are a strong Python type — but named tuples much more so!

Named tuples join the strengths of names and tuples. Photo by Ainur Iman on Unsplash

The three hottest Python data types are the list, the dictionary, and the tuple. Lists and dictionaries are mutable, meaning that their elements might be altered after creation. Tuples, alternatively, are immutable, in order that they can’t be modified after creation. When you do need to change the contents of a tuple, you could create a recent instance with the specified changes and assign it to the identical variable.

This text focuses on Python named tuples, a specialized form of tuple that mixes the ability of standard tuples with the added flexibility of named fields. In comparison with regular tuples, named tuples could make code simpler, more readable and more maintainable — and much more Pythonic. Nevertheless, you could watch out, as sometimes using named tuples excessively can inadvertently reduce code readability fairly than enhance it.

Read on to learn more!

To grasp named tuples, you’ve to first understand regular Python tuples. When you’re not aware of them, I strongly advise you to first read the next two articles about this data type:

What’s incredible about named tuples is that they function like regular tuples: all the pieces that works for a daily tuple will work for a named tuple. But that’s not all, as named tuples offer additional features — hence the moniker “tuple+”. Subsequently, I’ll assume you’re aware of the important thing concepts covered in these two articles above, and we’ll deal with the benefits of named tuples.

Initially, take note that every one tuples are immutable. You might find it easy to forget this significant characteristic whenever you start…

LEAVE A REPLY

Please enter your comment!
Please enter your name here