Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a defaultdict whose values are defaultdict

Is there any particular reason that my application says the argument must be callable or none type? I'm pretty sure this is how you'd instantiate a defaultdict with a defaultdict as its values.

dict = defaultdict(defaultdict(set))
like image 761
Jacob Macallan Avatar asked Aug 16 '26 19:08

Jacob Macallan


1 Answers

The argument provided to the defaultdict constructor must be a zero-argument callable (see the docs), so defaultdict(defaultdict) does work, but defaultdict(defaultdict(set)) does not. You can 'cheat' a little though:

d = defaultdict(lambda: defaultdict(set))

That way you provide a zero-argument callable in the form of the lambda function which in turn, when called, returns the appropriate default value you want.

like image 52
user2390182 Avatar answered Aug 19 '26 08:08

user2390182



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!