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]}