'For each dict in list, extract data into another list of dict
I've been trying to find a way to do this, but can't seem to find a solution.
I have a list that contains a load of dictionaries, I want to go through each dictionary in the list to extract the values to form a smaller list contains the data in need.
The data is formed:
[{'q_rounded': 100, 'title': 'Product Evaluation', 'final_score': 5.0, 'project': <Project: C>},{'q_rounded': 100, 'title': 'Community', 'final_score': 5.0, 'project': <Project: C>},{'q_rounded': 100, 'title': 'Marketing', 'final_score': 5.0, 'project': <Project: C>},{'q_rounded': 0, 'title': 'Product Evaluation', 'project': <Project: D>}]
...
I want to group the final score for each title for each project into a single list
[{project_name, score 1, score 2, score 3...},
{project_name, score 1, score 2, score 3}
...
]
Is this even possible to do?
Update
My current list contains loads of dicts that show a score for an area of a project.
For example. Project1 has a score for 5 different areas and therefore my list of dicts has 5 seperate dicts just for this 1 project.
[{projectName1, Score 1},
{projectName1, score 2},
projectname1, score 3}...
}]
I'm trying to take each dictionary that relates to a project and build 1 list for each project
So i should end up with:
[{project1, score1, score2, score3
}...]
Which will end up giving me a list of dicts that will contain a single dict for every project that contains all the scores.
[{project1, score1, score2, score3
},
{project2, score1, score2, score3...]
The original list of dicts is made by:
results.append({'title':q.title,"project_image":project.image,'final_score':final_score,"project_name":project.name,"project_id":project.id})
Update
I'm trying to end up with a list of the project, and its values for each area. Not a dict.
Solution 1:[1]
I'm guessing scores are an inner property of project object and you can try using list comprehension to do something like this:
output_list = [{'project_name':inner_dict.get('title'), 'scores': [inner_dict.get('project').scores]} for inner_dict in original_list]
Solution 2:[2]
You could loop over all dictionaries and get the required info. Based on the output example, I would however store them in a dictionary of lists instead of a list of dictionaries (see example below).
Used input:
input = [{'q_rounded': 100,
'title': 'Product Evaluation',
'final_score': 5.0,
'project': "C"},
{'q_rounded': 100,
'title': 'Community',
'final_score': 5.0,
'project': "C"},
{'q_rounded': 100,
'title': 'Marketing',
'final_score': 5.0,
'project': "C"},
{'q_rounded': 0,
'title': 'Product Evaluation',
'final_score': 3.0,
'project': "D"}]
Code:
output = {}
for entry in input:
name = entry["project"]
if name in output:
output[name].append(entry["final_score"])
else:
output[name]= [entry["final_score"]]
Resulting output:
output = {'C': [5.0, 5.0, 5.0], 'D': [3.0]}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Elad L. |
| Solution 2 | Steven Robyns |
