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

Michael Selik mike at selik.org
Thu Jun 28 19:38:21 EDT 2018


On Thu, Jun 28, 2018 at 4:34 PM Chris Barker via Python-ideas <
python-ideas at python.org> wrote:

> On Thu, Jun 28, 2018 at 4:23 PM, Greg Ewing <greg.ewing at canterbury.ac.nz>
> wrote:
>
>> Nicolas Rolin wrote:
>>
>>>     student_by_school = {group_by(school): student for school, student
>>> in student_school_list}
>>>
>>
>> In the spirit of making the target expression look like
>> a template for the generated elements,
>>
>>    {school: [student...] for school, student in student_school_list}
>
>
> hmm -- this seems a bit non-general -- would this only work for a list?
> maybe you would want a set, or???
>
> so could be get a defaultdict comprehension with something like:
>
> { school: (default_factory=list, student) for school, student in
> student_school_list }
>
> But I can't think of an reasonable syntax to make that work.
>

Many languages with a group-by or grouping function choose to return a
mapping of sequences, requiring any reduction, aggregation, or
transformation of those sequences to be performed after the grouping.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180628/3cdfb089/attachment.html>


More information about the Python-ideas mailing list