[Python-ideas] Where should grouping() live
Steven D'Aprano
steve at pearwood.info
Tue Jul 3 20:21:03 EDT 2018
On Wed, Jul 04, 2018 at 10:44:17AM +1200, Greg Ewing wrote:
> Steven D'Aprano wrote:
> >I propose that a better name which indicates the non-lazy nature of this
> >function is *grouped* rather than grouping, like sorted().
>
> +1
>
> >As for where it belongs, perhaps the collections module is the least
> >worst fit.
>
> But then there's the equally strong purist argument that it's
> not a data type, just a function.
Yes, I realised that after I posted my earlier comment.
> Unless we *make* it a data type. Then not only would it fit
> well in collections, it would also make it fairly easy to do
> incremental grouping if you really wanted that.
>
> Usual case:
>
> g = groupdict((key(val), val) for val in things)
How does groupdict differ from regular defaultdicts, aside from the
slightly different constructor?
> Incremental case:
>
> g = groupdict()
> for key(val), val in things:
> g.add(key, val)
> process_partial_grouping(g)
I don't think that syntax works. I get:
SyntaxError: can't assign to function call
Even if it did work, it's hardly any simpler than
d = defaultdict(list)
for val in things:
d[key(val)].append(val)
But then Counter is hardly any simpler than a regular dict too.
--
Steve
More information about the Python-ideas
mailing list