<div dir="ltr">I admit a hypothetical itertools.grouping that returned incrementally built dictionaries doesn't fill any simple need I have often encountered. I can be hand-wavy about "stateful bucketing of streams" and looking at windowing/tails, but I don't have a clean and simple example where I need this. The "run to exhaustion" interface has more obvious uses (albeit, they *must* be technically a subset of the incremental ones).<div><br></div><div>I think I will also concede that in incrementally built and yielded dictionary isn't *really* in the spirit of itertools either. I suppose tee() can grow unboundedly if only one tine is utilized... but in general, itertools is meant to provide iterators that keep memory usage limited to a few elements in memory at a time (yes, groupby, takewhile, or dropwhile have pathological cases that could be unbounded... but usually they're not).</div><div><br></div><div>So maybe we really do need a dicttools or mappingtools module, with this as the first function to put inside it.</div><div><br></div><div>... but I STILL like a new collections.Grouping (or collections.Grouper) the best. It might overcome Guido's reluctance... and what goes there is really delegated by him, not his own baby.<br><br><div class="gmail_quote"><div dir="ltr">On Tue, Jul 3, 2018 at 12:19 PM Chris Barker via Python-ideas <<a href="mailto:python-ideas@python.org">python-ideas@python.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Jul 3, 2018 at 8:24 AM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On Tue, Jul 03, 2018 at 09:23:07AM -0400, David Mertz wrote:<br></span></blockquote><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>> My problem with the second idea is that *I* find it very wrong to have<br>
> something in itertools that does not return an iterator. It wrecks the<br>
> combinatorial algebra of the module.<br></span></blockquote><div><br></div><div>hmm -- that seems to be a pretty pedantic approach -- practicality beats purity, after all :-)</div><div><br></div><div>I think we should first decide if a grouping() function is a useful addition to the standard library (after all: "not every two line function needs to in the stdlib"), and f so, then we can find a home for it.</div><div><br></div><div>personally, I'm wondering if a "dicttools" or something module would make sense -- I imagine there are all sorts of other handy utilities for working with dicts that could go there. (though, yeah, we'd want to actually have a handful of these before creating a new module :-) )</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>> That said, it's easy to fix... and I believe independently useful. Just<br>
> make grouping() a generator function rather than a plain function. This<br>
> lets us get an incremental grouping of an iterable.<br>
<br>
</span>We already have something which lazily groups an iterable, returning <br>
groups as they are seen: groupby.<br>
<br>
What makes grouping() different from groupby() is that it accumulates <br>
ALL of the subgroups rather than just consecutive subgroupings. </blockquote><div><br></div><div>well, yeah, but it wont actually get you those until you exhaust the iterator -- so while it's different than itertools.groupby, it is different than itertools.groupby(sorted(iterable))?</div><div><br></div><div>In short, this wouldn't really solve the problems that itertools.groupby has for this sort of task -- so what's the point?</div><div><br></div><div> > As for where it belongs, perhaps the collections module is the least worst fit.</div><div><br></div><div>That depends some on whether we go with a simple function, in which case collections is a pretty bad fit (but maybe still the least worse).</div><div><br></div><div>Personally I still like the idea of having this be special type of dict, rather than "just a function" -- and then it's really obvious where to put it :-)</div><div><br></div><div>-CHB</div><div><br></div><div> </div></div>-- <br><div class="m_-3446902231165398649gmail_signature" data-smartmail="gmail_signature"><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&R (206) 526-6959 voice<br>7600 Sand Point Way NE (206) 526-6329 fax<br>Seattle, WA 98115 (206) 526-6317 main reception<br><br><a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a></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><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>advocates of freedom in prisons. Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br></div></div></div>