On 2018-07-03 23:20, Greg Ewing wrote:
Nicolas Rolin wrote:
grouping(((len(word), word) for word in words))
That actually has one more level of parens than are needed, you can just write
grouping((len(word), word) for word in words)
FWIW, here's my opinion.
I much prefer something like:
I think that building an iterable of 2-tuples to pass to 'grouped' is much like following a decorate-sort-undecorate pattern when sorting, or something similar when using 'min' or 'max'. Passing an iterable of items and optionally a key function is simpler, IMHO.
Why would you pass 2-tuples, anyway?
Maybe it's because 'grouped' returns a dict and a dict can be built from an iterable of 2-tuples, but that's OK because a dict needs key/value pairs.
When 'Counter' was being proposed, it was suggested that one could be created from an iterable of 2-tuples, which sort of made sense because a Counter is like a dict, but, then, how would you count 2-tuples?
Fortunately, Counter counts items, so you can do things like:
counts = Counter(list_of_words)
I think it's the same thing here.
'grouped' returns a dict, so passing 2-tuples initially seems reasonable, but, as in the case with Counter, I think it would be a mistake.
It would be nice to be able to say:
grouped((word.casefold(), word) for word in words)
It would match the pattern of sorted, min and max.