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

Chris Barker chris.barker at noaa.gov
Thu Jun 28 13:01:00 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).
>

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


-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180628/59241d25/attachment.html>


More information about the Python-ideas mailing list