Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Divyanshu Shekhar
Divyanshu Shekhar

Posted on

     

Python Named Tuple

A named tuple in python is a subclass of tuples. The named tuple has the same functionalities as a normal tuple, but its values can be accessed both by name (using dot notation eg: .name) as well as by position (offset notation eg: [0]). In this blog, we will learn what is Named Tuple in python, its syntax, and functions with the help of examples.

Why Do We Need Namedtuple In Python?

One obvious question that will be on your mind is, “Why do we need Named Tuple in Python when we have a standard tuple?“.

The response to this question is that the named tuple improves the readability of the code and functions as a self-documented code.

Let’s understand it via an example:

Suppose we are setting up a software that stores student subject marks in a tuple called ‘marks.’ But, this code is readable only when the tuple description is in our memory. We are likely to forget the tuple definition after two or three days.

Python Tuple

marks = (98, 80, 95)print(marks)print(marks[0])print(marks[2])(98, 80, 95)9895
Enter fullscreen modeExit fullscreen mode

Named Tuple comes as a substitute for the regular tuple, which functions the same as the usual tuple, but improves the readability of our code. And even if the creator takes a break of one or two months, the syntax or meaning of the named tuple will speak for itself.

That’s what we need as a developer, a self-documented code.

Enough talking about Named Tuple. It’s high time to introduce you to its syntax.

Learn aboutPython Named Tuple from the Original Post.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Developer | Blogger
  • Joined

More fromDivyanshu Shekhar

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp