There are some cases when that's the correct behavior. It mimics pandas.DataFrame.groupby. For example, what if you have a sequence of (key, v1, v2) triples? Group by key, then keep the triples intact is the right choice sometimes.<div><br><br><div class="gmail_quote"><div dir="ltr">On Wed, Jul 4, 2018, 6:39 AM David Mertz <<a href="mailto:mertz@gnosis.cx">mertz@gnosis.cx</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div class="gmail_quote" dir="auto"><div dir="ltr">Steven:<br></div><div dir="auto"><div dir="auto"><br></div><div dir="auto">You've misunderstood part of the discussion. There are two different signatures being discussed/proposed for a grouping() function.</div><div dir="auto"><br></div><div dir="auto">The one you show we might call grouping_michael(). The alternate API we might call grouping_chris(). These two calls will produce the same result (the first output you show)</div><div dir="auto"><br></div><div dir="auto"><span style="font-family:sans-serif"> grouping_michael(words, keyfunc=len)</span><br style="font-family:sans-serif"><span style="font-family:sans-serif"> grouping_chris((len(word), word) for word in words)</span></div><div dir="auto"><font face="sans-serif"><br></font></div><div dir="auto"><font face="sans-serif">I happen to prefer grouping_michael(), but recognize they each make slightly different things obvious. Absolutely no one wants the behavior in your second output.<br></font></div></div></div></div><div dir="auto"><div class="gmail_quote" dir="auto"><div dir="auto"><div dir="auto"><br><div class="gmail_quote" dir="auto"><div dir="ltr">On Tue, Jul 3, 2018, 9:32 PM Steven D'Aprano <<a href="mailto:steve@pearwood.info" rel="noreferrer" target="_blank">steve@pearwood.info</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Of course you can prepare the sequence any way you like, but these are <br>
not equivalent:<br>
<br>
grouping(words, keyfunc=len)<br>
grouping((len(word), word) for word in words)<br>
<br>
The first groups words by their length; the second groups pairs of <br>
(length, word) tuples by equality.<br>
<br>
py> grouping("a bb ccc d ee fff".split(), keyfunc=len)<br>
{1: ['a', 'd'], 2: ['bb', 'ee'], 3: ['ccc', 'fff']}<br>
<br>
py> grouping((len(w), w) for w in "a bb ccc d ee fff".split())<br>
{(3, 'ccc'): [(3, 'ccc')], (1, 'd'): [(1, 'd')], (2, 'ee'): [(2, 'ee')], <br>
(3, 'fff'): [(3, 'fff')], (1, 'a'): [(1, 'a')], (2, 'bb'): [(2, '<br></blockquote></div></div></div></div></div>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div></div>