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.
The Student age using index is : 19 The Student name using keyname is : Nandini
Let’s see various Operations on namedtuple() :
- Access by index : The attribute values of namedtuple() are ordered and can be accessed using the index number unlike dictionaries which are not accessible by index.
- Access by keyname : Access by keyname is also allowed as in dictionaries.
- using getattr() :- This is yet another way to access the value by giving namedtuple and key value as its argument.
The Student age using index is : 19 The Student name using keyname is : Nandini The Student DOB using getattr() is : 2541997
- _make() :- This function is used to return a namedtuple() from the iterable passed as argument.
- _asdict() :- This function returns the OrderedDict() as constructed from the mapped values of namedtuple().
- using “**” (double star) operator :- This function is used to convert a dictionary into the namedtuple().
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')
- _fields :- This function is used to return all the keynames of the namespace declared.
- _replace() :- This function is used to change the values mapped with the passed keyname.
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 firstname.lastname@example.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.
- How to use NamedTuple and Dataclass in Python?
- Difference between DataClass vs NamedTuple vs Object in Python
- typing.NamedTuple – Improved Namedtuples
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Python - Read blob object in python using wand library
- Python | PRAW - Python Reddit API Wrapper
- twitter-text-python (ttp) module - Python
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python program to check if the list contains three consecutive common numbers in Python