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

Nicolas Rolin nicolas.rolin at tiime.fr
Thu Jun 28 21:45:50 EDT 2018

2018-06-28 22:34 GMT+02:00 David Mertz <mertz at gnosis.cx>:

> I agree with these recommendations. There are excellent 3rd party tools
> that do what you want. This is way too much to try to shoehorn into a
> comprehension.

There are actually no 3rd party tools that can "do what I want", because if
I wanted to have a function to do a group by, I would have taken the 5
minutes and 7 lines necessary to do so (or don't use a function and do my 3

My main point is that comprehensions in python are very powerful and you
can do pretty much any basic data manipulation that you want with it EXCEPT
when you want to "split" a list in sublists, in which case you have either
to use functions or a for loop.
You can note that with list comprehension you can flatten an iterable (from
sublists to a single list) with the [a for b in c for a in b] syntax, but
doing the inverse operation is impossible.

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 ?
- In which case, is there a way to find a simple syntax that is not too
confusing ?

My personal answer would be respectively "yes" and "maybe I don't know".
I was hoping to have some views on the topic, and it seemed to have a bit
sidetracked :)

Nicolas Rolin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180629/18d9a720/attachment.html>

More information about the Python-ideas mailing list