> For the complete documentation index, see [llms.txt](https://gyansetu-python.gitbook.io/python-programming/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gyansetu-python.gitbook.io/python-programming/advanced-data-types/python-namedtuples.md).

# Python namedTuples

Python supports a type of container like dictionaries called “**namedtuples()**” present in module, “**collection**“. 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.

**Operations on namedtuple() :Access Operations**

**1. 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.

**2. Access by keyname :** Access by keyname is also allowed as in dictionaries.

**3. using getattr() :-** This is yet another way to access the value by giving namedtuple and key value as its argument.

**Example 1:**

```python
# 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')) 
```

**Output:**

The Student age using index is : 19&#x20;

The Student name using keyname is : Nandini&#x20;

The Student DOB using getattr() is : 2541997

**Conversion Operations**

**1. \_make() :-** This function is used to return a **namedtuple() from the iterable** passed as argument.

**2. \_asdict() :-** This function returns **the OrdereDict()** as constructed from the mapped values of namedtuple().

**3. using “\*\*” (double star) operator** :- This function is used to **convert a dictionary into the namedtuple().**

**Example 2:**

```python
# 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)) 
```

**Output:**

The namedtuple instance using iterable is :&#x20;

Student(name='Manjeet', age='19', DOB='411997')&#x20;

The OrderedDict instance using namedtuple is :&#x20;

OrderedDict(\[('name', 'Nandini'), ('age', '19'), ('DOB', '2541997')])&#x20;

The namedtuple instance from dict is :&#x20;

Student(name='Nikhil', age=19, DOB='1391997')

**Additional Operations**

**1. \_fields :-** This function is used to return **all the keynames** of the namespace declared.

**2. \_replace() :-** This function is used to **change the values** mapped with the passed keyname.

**Example 3:**

```python
# 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')) 
```

**Output:**

All the fields of students are :&#x20;

('name', 'age', 'DOB')&#x20;

The modified namedtuple is :&#x20;

Student(name='Manjeet', age='19', DOB='2541997')


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gyansetu-python.gitbook.io/python-programming/advanced-data-types/python-namedtuples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
