Fwd: Allow a group by operation for dict comprehension

Mike Selik asked for my opinion on a draft PEP along these lines. I proposed a slight modification to his idea that is now reflected in his latest edits. With some details fleshed out, I think this is a promising idea. I like the a collections class better, of course, but a dict classmethod is still a lot smaller change than new syntax change in comprehension. On Thu, Jun 28, 2018, 8:15 PM David Mertz <mertz@gnosis.cx> wrote:

On 2018-06-29 05:14, David Mertz wrote:
[snip]
I think that if a Grouper instance is created with a key function, then that key function should be used by the .update method. You _could_ possibly override that key function by providing a new one when updating, but, OTOH, why would you want to? You'd be mixing different kinds of groupings! So -1 on that.
If you want to avoid duplicates, maybe the grouper should be created with 'set' as the default factory (see 'defaultdict'). However, there's the problem that 'list' has .append but 'set' has .add...
[snip]

I created a separate thread to continue this discussion: "grouping / dict of lists" https://github.com/selik/peps/blob/master/pep-9999.rst In my proposal, the update offers a key-function in case the new elements don't follow the same pattern as the existing ones. I can understand the view that the class should retain the key-function from initialization. The issue of the group type -- list, set, Counter, etc. -- is handled by offering a Grouping.aggregate method. The Grouping class creates lists, which are passed to the aggregate function. I included examples of constructing sets and Counters. On Fri, Jun 29, 2018 at 10:04 AM MRAB <python@mrabarnett.plus.com> wrote:

On 2018-06-29 05:14, David Mertz wrote:
[snip]
I think that if a Grouper instance is created with a key function, then that key function should be used by the .update method. You _could_ possibly override that key function by providing a new one when updating, but, OTOH, why would you want to? You'd be mixing different kinds of groupings! So -1 on that.
If you want to avoid duplicates, maybe the grouper should be created with 'set' as the default factory (see 'defaultdict'). However, there's the problem that 'list' has .append but 'set' has .add...
[snip]

I created a separate thread to continue this discussion: "grouping / dict of lists" https://github.com/selik/peps/blob/master/pep-9999.rst In my proposal, the update offers a key-function in case the new elements don't follow the same pattern as the existing ones. I can understand the view that the class should retain the key-function from initialization. The issue of the group type -- list, set, Counter, etc. -- is handled by offering a Grouping.aggregate method. The Grouping class creates lists, which are passed to the aggregate function. I included examples of constructing sets and Counters. On Fri, Jun 29, 2018 at 10:04 AM MRAB <python@mrabarnett.plus.com> wrote:
participants (3)
-
David Mertz
-
Michael Selik
-
MRAB