Operations on namedtuple ():
1. Index access: the namedtuple () attribute values are ordered and can be accessed using the index number, as opposed to dictionaries, which are not indexed.
2. Keyword access. Keyword access is also allowed, as in dictionaries.
3. using getattr (): — this is another way to access the value by passing namedtuple and the key value as an argument.
The Student age using index is: 19 The Student name using keyname is: Nandini The Student DOB using getattr () is: 2541997
1. _make (): — This function is used to return a namedtuple () from a repeatable argument passed as an argument.
2. _asdict (): — This function returns : — This function is used to convert a dictionary to 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')
1. _fields: — This function is used to return all key names of the declared namespace.
2. _replace (): — This function is used to change the values associated with the passed key name.
All the fields of students are: ('name',' age', 'DOB' ) The modified namedtuple is: Student (name = 'Manjeet', age =' 19', DOB = '2541997')
This article is courtesy of Manjeet Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.
Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.