Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Convert List of named tuples to dictionary

Improve Article
Save Article
Like Article
  • Last Updated : 23 Mar, 2023
Improve Article
Save Article
Like Article

In this article, we will convert a list of the named tuples to dictionaries using python.

By using dict() method we can convert a list of named tuples to the dictionary. Before that, we have to convert it into a dictionary using the _asdict() method. 

First, we have to convert namedtuple to the dictionary in each element by using _asdict() method and then we finally convert to the dictionary by using dict() method

Syntax:

for i in list:
     print(dict(i._asdict()))

where, the list is a named tuple

Example: Python program to convert namedtuple to the dictionary

Python3




# import named tuple
from collections import namedtuple
 
# create a named tuple named DETAILS with three columns
DETAILS = namedtuple("DETAILS", "Name, Age, Subject")
 
# create 5 students
a = [DETAILS("ojaswi", 21, "python"),
     DETAILS("sireesha", 21, "python"),
     DETAILS("gnanesh", 23, "php"),
     DETAILS("priyank", 21, "java"),
     DETAILS("ojaswi", 22, "big-data")]
 
# convert into dictionary
# using dict method
for i in a:
    print(dict(i._asdict()))

Output:

{‘Name’: ‘ojaswi’, ‘Age’: 21, ‘Subject’: ‘python’}

{‘Name’: ‘sireesha’, ‘Age’: 21, ‘Subject’: ‘python’}

{‘Name’: ‘gnanesh’, ‘Age’: 23, ‘Subject’: ‘php’}

{‘Name’: ‘priyank’, ‘Age’: 21, ‘Subject’: ‘java’}

{‘Name’: ‘ojaswi’, ‘Age’: 22, ‘Subject’: ‘big-data’}

Example: Python program to convert namedtuple to a dictionary

Python3




# import named tuple
from collections import namedtuple
 
# create a named tuple named DETAILS with one column
DETAILS = namedtuple("DETAILS", "Name")
 
# create 5 students
a = [DETAILS("ojaswi"),
     DETAILS("sireesha"),
     DETAILS("gnanesh"),
     DETAILS("priyank"),
     DETAILS("ojaswi")]
 
# convert into dictionary
# using dict method
for i in a:
    print(dict(i._asdict()))

Output:

{‘Name’: ‘ojaswi’}

{‘Name’: ‘sireesha’}

{‘Name’: ‘gnanesh’}

{‘Name’: ‘priyank’}

{‘Name’: ‘ojaswi’}

Method 3: Creating a List of Dictionaries to Store Student Details.

Defines an empty list and dictionary to store the details, iterates through the list of student names, adds key-value pairs to the dictionary for each student, converts the student’s details into a dictionary using the dict() function, and appends the student’s dictionary to the list. Finally, it prints the dictionary for each student.

  1. Define an empty list called a to store the student details.
  2. Define a dictionary called student_details to store the keys and values for each student.
  3. Using a for loop, iterate through each student name in the list of names.
  4. For each name, add a new key-value pair to the student_details dictionary with the student name as the key and an empty dictionary as the value.
  5. Use another for loop to iterate through each student in the student_details dictionary.
  6. For each student, add the key-value pair “Name” and the corresponding name to the dictionary.
  7. Convert the student’s details into a dictionary using the dict() function.
  8. Print the dictionary for each student.

Python3




# Define an empty list to store the student details
a = []
 
# Define a dictionary to store the keys and values for each student
student_details = {}
 
# Iterate through each student name
for name in ["ojaswi", "sireesha", "gnanesh", "priyank", "ojaswi"]:
 
    # Add a new key-value pair to the dictionary for each student name
    student_details[name] = {}
 
    # Add the student's name to the dictionary
    student_details[name]["Name"] = name
 
    # Convert the student's details into a dictionary using the dict() function
    student_dict = dict(student_details[name])
 
    # Add the student's dictionary to the list of student details
    a.append(student_dict)
 
# Print the dictionary for each student
for i in a:
    print(i)

Output

{'Name': 'ojaswi'}
{'Name': 'sireesha'}
{'Name': 'gnanesh'}
{'Name': 'priyank'}
{'Name': 'ojaswi'}

Time complexity: O(n), where n is the number of students.
Auxiliary space: O(n), to store the list of student details and the dictionary for each student.


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!