On Fri, Nov 2, 2018 at 12:07 PM Alex Shafer <ashafe...@gmail.com> wrote:
> Other APIs I've considered for this are a new keyword argument to the 
> existing `setdefault()`, or perhaps more radically for Python, a new keyword 
> argument to the `dict()` constructor that would get called as an implicit 
> default for `setdefault()` and perhaps used in other scenarios (essentially 
> defining a type for dict values).
>

The time machine has been put to good use here. Are you aware of
__missing__ and collections.defaultdict? You just create a defaultdict
with a callable (very common to use a class like "list"), and any time
you try to use something that's missing, it'll call that to generate a
value.

from collections import defaultdict
d = defaultdict(list)
for category, item in some_stuff:
    d[category].append(item)

Easy way to group things into their categories.

ChrisA
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to