
On Thu, Jun 28, 2018 at 8:25 AM, Nicolas Rolin nicolas.rolin@tiime.fr wrote:
I use list and dict comprehension a lot, and a problem I often have is to do the equivalent of a group_by operation (to use sql terminology).
I don't know from SQL, so "group by" doesn't mean anything to me, but this:
For example if I have a list of tuples (student, school) and I want to have the list of students by school the only option I'm left with is to write
student_by_school = defaultdict(list) for student, school in student_school_list: student_by_school[school].append(student)
seems to me that the issue here is that there is not way to have a "defaultdict comprehension"
I can't think of syntactically clean way to make that possible, though.
Could itertools.groupby help here? It seems to work, but boy! it's ugly:
In [*45*]: student_school_list
Out[*45*]:
[('Fred', 'SchoolA'),
('Bob', 'SchoolB'),
('Mary', 'SchoolA'),
('Jane', 'SchoolB'),
('Nancy', 'SchoolC')]
In [*46*]: {a:[t[0] *for* t *in* b] *for* a,b *in* groupby(sorted(student_school_list, key=*lambda* t: t[1]), key=*lambda* t: t[
...: 1])}
...:
...:
...:
...:
...:
...:
...:
Out[*46*]: {'SchoolA': ['Fred', 'Mary'], 'SchoolB': ['Bob', 'Jane'], 'SchoolC': ['Nancy']}
-CHB