
On Fri, Jun 29, 2018 at 10:53:34AM -0700, Michael Selik wrote:
Hello,
I've drafted a PEP for an easier way to construct groups of elements from a sequence. https://github.com/selik/peps/blob/master/pep-9999.rst
Seems useful, but I suggest that since it has to process the entire data set eagerly, the name ought to be grouped() following the precedent set by sorted().
I also suggest using keyfunc as the second parameter, following the same convention as itertools.groupby. That gives this possible implementation:
def grouped(iterable, keyfunc=None): groups = {} for k, g in itertools.groupby(iterable, keyfunc): groups.setdefault(k, []).extend(g) return groups
Since Guido has ruled out making this a built-in, there's no really comfortable place in the standard library for it:
- it doesn't return an iterator (since it is eager, it would be pointless to yield key/items pairs instead of just returning the dict), so itertools is not a good fit;
- it doesn't return a specialist class, so collections is not a good fit;
- there's currently no "useful utilities which aren't useful enough to be built-in" module.
I fear that this proposal will fall into that awkward position of being doomed by not having somewhere to put it.
(Your suggestion to consider this an alternate constructor of dicts seems more sensible all the time... but again Guido disagrees.)