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

Nicolas Rolin nicolas.rolin at tiime.fr
Fri Jun 29 05:04:20 EDT 2018


A syntax that would work (which atm is a syntax error, and requires no new
keyword) would be

    student_by_school = {school: [student] for school, student in
student_school_list, grouped=True}

with grouped=True being a modifier on the dict comprehension so that at
each iteration loop

current_dict[key] = value if key not in current_dict else current_dict[key]
+ value

This is an extremely borderline syntax (as it is perfectly legal to put
**{'grouped': True} in a dict comprehension), but it works.
It even keeps the extremely important "should look like a template of the
final object" property.

But it doesn't requires me to defines 2 lambda functions just to do the job
of a comprehension.

-- 
Nicolas Rolin


2018-06-29 4:57 GMT+02:00 Michael Selik <mike at selik.org>:

> On Thu, Jun 28, 2018, 6:46 PM Nicolas Rolin <nicolas.rolin at tiime.fr>
> wrote:
>
>> The questions I should have asked In my original post was :
>> - Is splitting lists into sublists (by grouping elements) a high level
>> enough construction to be worthy of a nice integration in the comprehension
>> syntax ?
>>
>
> My intuition is no, it's not important enough to alter the syntax, despite
> being an important task.
>
> - In which case, is there a way to find a simple syntax that is not too
>> confusing ?
>>
>
> If you'd like to give it a shot, try to find something which is currently
> invalid syntax, but does not break compatibility. The latter criteria means
> no new keywords. The syntax should look nice as a single line with
> reasonably verbose variable names.
>
> One issue is that Python code is mostly 1-dimensional, characters in a
> line, and you're trying to express something which is 2-dimensional, in a
> sense. There's only so much you can do without newlines and indentation.
>



-- 

--
*Nicolas Rolin* | Data Scientist
+ 33 631992617 - nicolas.rolin at tiime.fr <prenom.nom at tiime.fr>


*15 rue Auber, **75009 Paris*
*www.tiime.fr <http://www.tiime.fr>*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180629/94777c76/attachment.html>


More information about the Python-ideas mailing list