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

David Mertz mertz at gnosis.cx
Thu Jun 28 20:24:07 EDT 2018


I think you cheated a little in your cut-and-paste.  `student_by_school` is
not defined in the code you've shown.  What you **did** define, `
student_school_list` doesn't give you what you want if you use `
defaultdict(list,student_school_list)`.

I thought for a moment I might just use:

[(b,a) for a,b in student_school_list]


But that's wrong for reasons that are probably obvious to everyone else.
I'm not really sure what `student_by_school` could possibly be to make this
work as shown.

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

> In [97]: student_school_list
> Out[97]:
> [('Fred', 'SchoolA'),
>  ('Bob', 'SchoolB'),
>  ('Mary', 'SchoolA'),
>  ('Jane', 'SchoolB'),
>  ('Nancy', 'SchoolC')]
>
> In [98]: result = defaultdict(list, student_by_school)
>
> In [99]: result.items()
> Out[99]: dict_items([('SchoolA', ['Fred', 'Mary']), ('SchoolB', ['Bob',
> 'Jane']), ('SchoolC', ['Nancy'])])
>
> So: <small voice> never mind </small voice>
>
> -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
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180628/80a09577/attachment-0001.html>


More information about the Python-ideas mailing list