[Python-ideas] Allow a group by operation for dict comprehension

Michael Selik mike at selik.org
Thu Jun 28 11:38:10 EDT 2018


On Thu, Jun 28, 2018 at 8:25 AM Nicolas Rolin <nicolas.rolin at 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).
>
> 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)
>

Thank you for bringing this up. I've been drafting a proposal for a better
grouping / group-by operation for a little while. I'm not quite ready to
share it, as I'm still researching use cases.

I'm +1 that this task needs improvement, but -1 on this particular solution.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180628/1e748e2e/attachment.html>


More information about the Python-ideas mailing list