Python’s Collections Module: namedtuple

Introduction

As it has tuple in it’s name, one thing is clear that it will add some superpower?‍♀️ to the tuples.

namedtuple is a special type of tuple that has named indices. Ofcourse, they also have normal integer indices as they are tup…


This content originally appeared on DEV Community and was authored by Kathan Vakharia

Introduction

As it has tuple in it's name, one thing is clear that it will add some superpower?‍♀️ to the tuples.

namedtuple is a special type of tuple that has named indices. Ofcourse, they also have normal integer indices as they are tuples anway.

Importing namedtuple

from collections import namedtuple

namedtuple creation

# Step 1: Define namedtuple
# 'typename'=> used when printing an obj & low-lvl stuff
# 'fieldnames'=> list of names of indices
Student = namedtuple(typename='Student',
                     field_names=['fname', 'lname', 'age']
                     )

# Step 2: Create a namedtuple

# explicitly assigning named indices
s1 = Student(fname="Shan", lname="Patel", age=19)

# named indices are inferred
s2 = Student("Kathan", "Vakharia", 19)

print(s1, s2, sep='\n')

"""OUTPUT
Student(fname='Shan', lname='Patel', age=19)
Student(fname='Kathan', lname='Vakharia', age=19)
"""

? It is important to note that the identifier you use for defining the namedtuple(Student here) is the one you have to use while instantiating a namedtuple. It has nothing to do with the typename argument-It's just a name for your custom tuple sub-class.

Quoting the python documentation,

collections.namedtuple(typename, field_names,...)
- Returns a new tuple subclass named typename.
We can cross-verify if we want using issubclass function.

>>> issubclass(Student, tuple)
True
# CORRECT WAY
s0 = Student("heet", "vakharia")
print(s0)
"""OUTPUT
blah(fname='heet', lname='vakharia')
""""
# INCORRECT way
Student = namedtuple(typename="blah",
                         field_names=["fname", "lname"]
                        )
s1 = blah("kathan", "vakharia")
"""OUTPUT
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'blah' is not defined
"""

Indexing namedtuples

Indexing namedtuple object is very straight-forward. Here's how you do it,

# way1 : using integer indices
# Same like 'plain-tuples'
print("First element of s1: {}"
      .format(s1[0]))

# way 2: using named indices 'NEW'
print("Element of s2 having named index lname: {}"
      .format(s2.lname))
"""OUTPUT
First element of s1: Shan
Element of s2 having named index lname: Vakharia
"""

As it is a sub-class of tuple, it inherits all the methods and attributs of tuple.
However, there are some interesting methods pertaining to namedtuple. Let's see what are those,

classmethod somenamedtuple._make(iterable)

fields = ["John", "Doe", 34]
print(Student._make(fields))

"""OUTPUT
Student(fname='John', lname='Doe', age=34)
"""

somenamedtuple._asdict()

Returns a new dict which maps field names to their corresponding values,

from collections import namedtuple

Student = namedtuple(typename='Student',
                     field_names=['fname', 'lname', 'age'],
                     )

shan = Student(fname="Shan", lname="Patel", age=19)
kathan = Student("Kathan", "Vakharia", 19)

# namedtuple._asdict() 
print(shan._asdict())
print(kathan._asdict())

"""OUTPUT
{'fname': 'Shan', 'lname': 'Patel', 'age': 19}
{'fname': 'Kathan', 'lname': 'Vakharia', 'age': 19}
"""

somenamedtuple._replace(**kwargs)

Returns a new instance of the named tuple replacing specified fields with new values.

kathan = Student("Kathan", "Vakharia", 19)

# a new tuple will be created with changed fname
kathan_v2 = kathan._replace(fname="Karsh")

print(kathan_v2)

# Old kathan is still unchanged
print(kathan)

"""OUTPUT
Student(fname='Karsh', lname='Vakharia', age=19)
Student(fname='Kathan', lname='Vakharia', age=19)
"""

And that wraps our discussion on namedtuple. I have intentionally left some minor functionalities because they are too trivial :) However, feel free to check out the documentation for that remaining 5% stuff.


This content originally appeared on DEV Community and was authored by Kathan Vakharia


Print Share Comment Cite Upload Translate Updates
APA

Kathan Vakharia | Sciencx (2021-06-18T18:26:54+00:00) Python’s Collections Module: namedtuple. Retrieved from https://www.scien.cx/2021/06/18/pythons-collections-module-namedtuple/

MLA
" » Python’s Collections Module: namedtuple." Kathan Vakharia | Sciencx - Friday June 18, 2021, https://www.scien.cx/2021/06/18/pythons-collections-module-namedtuple/
HARVARD
Kathan Vakharia | Sciencx Friday June 18, 2021 » Python’s Collections Module: namedtuple., viewed ,<https://www.scien.cx/2021/06/18/pythons-collections-module-namedtuple/>
VANCOUVER
Kathan Vakharia | Sciencx - » Python’s Collections Module: namedtuple. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/18/pythons-collections-module-namedtuple/
CHICAGO
" » Python’s Collections Module: namedtuple." Kathan Vakharia | Sciencx - Accessed . https://www.scien.cx/2021/06/18/pythons-collections-module-namedtuple/
IEEE
" » Python’s Collections Module: namedtuple." Kathan Vakharia | Sciencx [Online]. Available: https://www.scien.cx/2021/06/18/pythons-collections-module-namedtuple/. [Accessed: ]
rf:citation
» Python’s Collections Module: namedtuple | Kathan Vakharia | Sciencx | https://www.scien.cx/2021/06/18/pythons-collections-module-namedtuple/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.