Home » NamedTuple in Python

NamedTuple in Python

Java SE 11 Programmer II [1Z0-816] Practice Tests
Java SE 11 Developer (Upgrade) [1Z0-817]
1 Year Subscription
Spring Framework Basics Video Course
Oracle Java Certification
Java SE 11 Programmer I [1Z0-815] Practice Tests

Named tuples assign meaning to each position in a tuple and allow for more readable, self-documenting code.

They can be used wherever regular tuples are used, and they add the ability to access fields by name instead of position index.

The namedtuple is an immutable container type, whose values can be accessed with indexes and named attributes. It has functionality like tuples with additional features.

A named tuple is created with the collections.namedtuple factory function.

Access Operations

Access the elements by using index
Access the elements using attribute name
Access the elements using getattr()

import collections as col  
# create Countries NamedTuple  
Countries = col.namedtuple('Country', ['name', 'capital', 'population'])  
# Add four Countries  
country1 = Countries('Germany', 'Berlin', '83190556')  
country2 = Countries('Spain', 'Madrid', '47450795')  
country3 = Countries('Italy', 'Rome', '60317116')  
country4 = Countries('France', 'Paris', '67413000')  
# Access the elements by using index  
print('The name and capital of country1: ' + country1[0] + ' and ' + country1[1])  
print('The name and capital of country2: ' + country2[0] + ' and ' + country2[1])  
# Access the elements using attribute name  
print('The name and capital of country3: ' + country3.name + ' and ' + country3.capital)  
print('The name and capital of country4: ' + country4.name + ' and ' + country4.capital)  
# Access the elements using getattr()  
print('The capital of country1 and country2: ' + getattr(country1, 'capital') + ' and ' + getattr(country2, 'capital'))  
print('The capital of country3 and country4: ' + getattr(country3, 'capital') + ' and ' + getattr(country4, 'capital'))

This displayed the following

>>> %Run namedtupleexample1.py
The name and capital of country1: Germany and Berlin
The name and capital of country2: Spain and Madrid
The name and capital of country3: Italy and Rome
The name and capital of country4: France and Paris
The capital of country1 and country2: Berlin and Madrid
The capital of country3 and country4: Rome and Paris

Lets look at another example

This demonstrates another couple of operations

_fields: This function is used to return all the keynames of the namespace declared.
_replace(): This is like str.replace() but targets named fields, it does modify the namedTuple it returns a new namedtuple

import collections as col  
# create Countries NamedTuple  
Countries = col.namedtuple('Country', ['name', 'capital', 'population'])  
# Add four Countries  
country1 = Countries('Germany', 'Berlin', '83190556')  
country2 = Countries('Spain', 'Madrid', '47450795')  
country3 = Countries('Italy', 'Rome', '60317116')  
country4 = Countries('France', 'Paris', '67413000')  

print(country1._fields) 
# ._replace returns a new namedtuple
print(country1._replace(name='Allemagne'))
# original namedtuple
print(country1)

This displayed the following

>>> %Run namedtupleexample2.py
('name', 'capital', 'population')
Country(name='Allemagne', capital='Berlin', population='83190556')
Country(name='Germany', capital='Berlin', population='83190556')

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More