Given a list of dictionaries:
players= [
{ "name": 'matt', 'school': 'WSU', 'homestate': 'CT', 'position': 'RB' },
{ "name": 'jack', 'school': 'ASU', 'homestate': 'AL', 'position': 'QB' },
{ "name": 'john', 'school': 'WSU', 'homestate': 'MD', 'position': 'LB' },
{ "name": 'kevin', 'school': 'ALU', 'homestate': 'PA', 'position': 'LB' },
{ "name": 'brady', 'school': 'UM', 'homestate': 'CA', 'position': 'QB' },
]
How can I group them into groups by matching their corresponding dictionary values so that they erupt:
Matching value 1:
name: [matt, john, kevin],
School: [WSU, WSU, ALU],
homestate: [CT, MD, PA]
position: [RB, LB, LB]
Matching value 2:
name: [jack, brady],
School: [ASU, UM],
homestate: [AL, CA]
position: [QB, QB]
Note that matching values are arbitrary; that is, it can be found anywhere. Maybe it's in schoolor in position, or maybe in both.
I tried to group them by doing:
from collections import defaultdict
result_dictionary = {}
for i in players:
for key, value in i.items():
result_dictionary.setdefault(key, []).append(value)
What issues:
{'name': ['matt', 'jack', 'john', 'kevin', 'brady'],
'school': ['WSU', 'ASU', 'WSU', 'ALU', 'UM'],
'homestate': ['CT', 'AL', 'MD', 'PA', 'CA'],
'position': ['RB', 'QB', 'LB', 'QB', 'QB']}
, , , , , .