👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
Thanks to some great folks on SO, I discovered the possibilities offered by collections.defaultdict
, notably in readability and speed. I have put them to use with success.
Now I would like to implement three levels of dictionaries, the two top ones being defaultdict
and the lowest one being int
. I don"t find the appropriate way to do this. Here is my attempt:
from collections import defaultdict
d = defaultdict(defaultdict)
a = [("key1", {"a1":22, "a2":33}),
("key2", {"a1":32, "a2":55}),
("key3", {"a1":43, "a2":44})]
for i in a:
d[i[0]] = i[1]
Now this works, but the following, which is the desired behavior, doesn"t:
d["key4"]["a1"] + 1
I suspect that I should have declared somewhere that the second level defaultdict
is of type int
, but I didn"t find where or how to do so.
The reason I am using defaultdict
in the first place is to avoid having to initialize the dictionary for each new key.
Any more elegant suggestion?
Thanks pythoneers!
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from Multiple levels of “collection.defaultdict” in Python, check other code Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Multiple levels of “collection.defaultdict” in Python
- Deutsch Multiple levels of “collection.defaultdict” in Python
- Français Multiple levels of “collection.defaultdict” in Python
- Español Multiple levels of “collection.defaultdict” in Python
- Türk Multiple levels of “collection.defaultdict” in Python
- Русский Multiple levels of “collection.defaultdict” in Python
- Português Multiple levels of “collection.defaultdict” in Python
- Polski Multiple levels of “collection.defaultdict” in Python
- Nederlandse Multiple levels of “collection.defaultdict” in Python
- 中文 Multiple levels of “collection.defaultdict” in Python
- 한국어 Multiple levels of “collection.defaultdict” in Python
- 日本語 Multiple levels of “collection.defaultdict” in Python
- हिन्दी Multiple levels of “collection.defaultdict” in Python
Paris | 2023-01-31
Simply put and clear. Thank you for sharing. Multiple levels of “collection.defaultdict” in Python and other issues with imp Python module was always my weak point 😁. I just hope that will not emerge anymore
Vigrinia | 2023-01-31
Simply put and clear. Thank you for sharing. Multiple levels of “collection.defaultdict” in Python and other issues with collections Python module was always my weak point 😁. I just hope that will not emerge anymore
Singapore | 2023-01-31
I was preparing for my coding interview, thanks for clarifying this - Multiple levels of “collection.defaultdict” in Python in Python is not the simplest one. Will use it in my bachelor thesis