[Python-ideas] Fwd: grouping / dict of lists

Chris Barker chris.barker at noaa.gov
Mon Jul 9 12:22:03 EDT 2018

On Fri, Jul 6, 2018 at 12:26 PM, Franklin? Lee I use this kind of function
in several different projects over the
> years, and I rewrote it many times as needed.

> I added several options, such as:
> - key function
> - value function
> - "ignore": Skip values with these keys.
> - "postprocess": Apply a function to each group after completion.
> - Pass in the container to store in. For example, create an
> OrderedDict and pass it in. It may already hold items.
> - Specify the container for each group.
> - Specify how to add to the container for each group.


> Then I cut it down to two optional parameters:
> - key function. If not provided, the iterable is considered to have
> key-value pairs.

OK -- seems we're all converging on that :-)

> - The storage container.

so this means you'r passing in a full set of storage containers? I'm a vit
confused by that -- if they might be pre-populated, then they would need to
be instance,s an you'd need to have one for every key -- how would you know
in advance aht you needed???

I played around with passing in a optional storage object:


but as we might want a list or a set, or a Counter, or ??? it got pretty
ugly, as lists and sets and Counters all have different APIs for adding
stuff. So I gave up and figured just saying "it's always a list) made the
most sense.

> Finally, I removed the key function, and only took pairs and an
> optional container. However, I don't remember why I removed the key
> function. It may be that I was writing throwaway lambdas, and I
> decided I might as well just write the transformation into the
> comprehension.

exactly -- but I suspect hat may be because you where writing a
comprehension anyway, as you needed to manipulate the values, also -- so if
there were a value function, you could use either API.

> I think a key function is worth having.

I think there's more or less consensus on that too.



Christopher Barker, Ph.D.

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180709/f060e7e2/attachment.html>

More information about the Python-ideas mailing list