Home > OS >  Vectorized approach for list of dictionaries
Vectorized approach for list of dictionaries

Time:06-29

Task is straightforward.

Input: list of dictionaries. Each dictionary contains two keys: class, studentid.

Output: Dictionary with key = class and value = list of studentids

So far, the best approach is for loop. However, I am wondering if this can be simplified (or made efficient) further. If similar form of problem can be solved outside Python, I am happy to learn.

from collections import defaultdict

students = [
    {'class': 1, 'studentid': 1},
    {'class': 1, 'studentid': 2},
    {'class': 2, 'studentid': 3},
    {'class': 2, 'studentid': 4},
    {'class': 3, 'studentid': 5}
]

classes = defaultdict(list)

for student in students:
    classno = student['class']
    classes[classno].append(student['studentid'])
        
# {1: [1, 2], 2: [3, 4], 3: [5]}

CodePudding user response:

Two options for using groupby are provided for reference

import pandas as pd

students = [
    {'class': 1, 'studentid': 1},
    {'class': 1, 'studentid': 2},
    {'class': 2, 'studentid': 3},
    {'class': 2, 'studentid': 4},
    {'class': 3, 'studentid': 5}
]

# Solution1 pandas
df = pd.DataFrame(students)
classes = {item[0]: item[1]["studentid"].to_list() for item in df.groupby("class")}
print(classes)

# Solution2 itertools
import itertools
from operator import itemgetter

classes = dict((k, [item["studentid"] for item in g]) for k, g in itertools.groupby(students, key=itemgetter('class')))
print(classes)
  • Related