The Wayback Machine - https://web.archive.org/web/20210209091758/https://www.geeksforgeeks.org/namedtuple-in-python/amp/

Namedtuple in Python

Python supports a type of container like dictionaries called “namedtuple()” present in module, “collections“. Like dictionaries they contain keys that are hashed to a particular value. But on contrary, it supports both access from key value and iteration, the functionality that dictionaries lack.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate namedtuple() 
      
from collections import namedtuple 
      
# Declaring namedtuple()  
Student = namedtuple('Student',['name','age','DOB'])  
      
# Adding values  
S = Student('Nandini','19','2541997')  
      
# Access using index  
print ("The Student age using index is : ",end ="")  
print (S[1])  
      
# Access using name   
print ("The Student name using keyname is : ",end ="")  
print (S.name)
chevron_right

Output:

The Student age using index is : 19
The Student name using keyname is : Nandini

Let’s see various Operations on namedtuple() :

Access Operations

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate namedtuple() and
# Access by name, index and getattr()
  
# importing "collections" for namedtuple()
import collections
  
# Declaring namedtuple()
Student = collections.namedtuple('Student',['name','age','DOB'])
  
# Adding values
S = Student('Nandini','19','2541997')
  
# Access using index
print ("The Student age using index is : ",end ="")
print (S[1])
  
# Access using name 
print ("The Student name using keyname is : ",end ="")
print (S.name)
  
# Access using getattr()
print ("The Student DOB using getattr() is : ",end ="")
print (getattr(S,'DOB'))
chevron_right

Output :

The Student age using index is : 19
The Student name using keyname is : Nandini
The Student DOB using getattr() is : 2541997

Conversion Operations

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate namedtuple() and
# _make(), _asdict() and "**" operator
  
# importing "collections" for namedtuple()
import collections
  
# Declaring namedtuple()
Student = collections.namedtuple('Student',['name','age','DOB'])
  
# Adding values
S = Student('Nandini','19','2541997')
  
# initializing iterable 
li = ['Manjeet', '19', '411997' ]
  
# initializing dict
di = { 'name' : "Nikhil", 'age' : 19 , 'DOB' : '1391997' }
  
# using _make() to return namedtuple()
print ("The namedtuple instance using iterable is  : ")
print (Student._make(li))
  
# using _asdict() to return an OrderedDict()
print ("The OrderedDict instance using namedtuple is  : ")
print (S._asdict())
  
# using ** operator to return namedtuple from dictionary
print ("The namedtuple instance from dict is  : ")
print (Student(**di))
chevron_right

Output :

The namedtuple instance using iterable is  : 
Student(name='Manjeet', age='19', DOB='411997')
The OrderedDict instance using namedtuple is  : 
OrderedDict([('name', 'Nandini'), ('age', '19'), ('DOB', '2541997')])
The namedtuple instance from dict is  : 
Student(name='Nikhil', age=19, DOB='1391997')

Additional Operations

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate namedtuple() and
# _fields and _replace()
  
# importing "collections" for namedtuple()
import collections
  
# Declaring namedtuple()
Student = collections.namedtuple('Student',['name','age','DOB'])
  
# Adding values
S = Student('Nandini','19','2541997')
  
# using _fields to display all the keynames of namedtuple()
print ("All the fields of students are : ")
print (S._fields)
  
# using _replace() to change the attribute values of namedtuple
print ("The modified namedtuple is : ")
print(S._replace(name = 'Manjeet'))
chevron_right

Output :

All the fields of students are : 
('name', 'age', 'DOB')
The modified namedtuple is : 
Student(name='Manjeet', age='19', DOB='2541997')

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




Article Tags :