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
from collections import namedtuple
DETAILS = namedtuple("DETAILS", "Name, Age, Subject")
a = [DETAILS("ojaswi", 21, "python"),
DETAILS("sireesha", 21, "python"),
DETAILS("gnanesh", 23, "php"),
DETAILS("priyank", 21, "java"),
DETAILS("ojaswi", 22, "big-data")]
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
from collections import namedtuple
DETAILS = namedtuple("DETAILS", "Name")
a = [DETAILS("ojaswi"),
DETAILS("sireesha"),
DETAILS("gnanesh"),
DETAILS("priyank"),
DETAILS("ojaswi")]
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.
- Define an empty list called a to store the student details.
- Define a dictionary called student_details to store the keys and values for each student.
- Using a for loop, iterate through each student name in the list of names.
- 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.
- Use another for loop to iterate through each student in the student_details dictionary.
- For each student, add the key-value pair “Name” and the corresponding name to the dictionary.
- Convert the student’s details into a dictionary using the dict() function.
- Print the dictionary for each student.
Python3
a = []
student_details = {}
for name in ["ojaswi", "sireesha", "gnanesh", "priyank", "ojaswi"]:
student_details[name] = {}
student_details[name]["Name"] = name
student_dict = dict(student_details[name])
a.append(student_dict)
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.
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
23 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...