I know similar questions have been asked before but I cannot find an adequate answer to my situation.
Let's say I have the following list of tuples:
d = [('first', 1), ('second', 2), ('third', 3)]
I can convert this very easily into a dictionary:
dict(d)
# {'first': 1, 'second': 2, 'third': 3}
Now, if I instead have the following list of tuples:
d = [('a', 'first', 1), ('a', 'second', 2), ('b', 'third', 3)]
How can I, most efficiently, get the following nested dictionary:
{'a': {'first': 1, 'second': 2}, 'b': {'third': 3}}
Here's the solution I have now:
from collections import defaultdict
dd = defaultdict(dict)
for a, b, c in d:
dd[a][b] = c
# defaultdict(dict, {'a': {'first': 1, 'second': 2}, 'b': {'third': 3}})
Is this the most performant way of doing this? Is it possible to avoid the for loop?
It's likely that I have to deal with cases where d
is very large, and this method may not scale very well. This part is critical to a web application I am building, and that's why performance is very important.
Input/feedback/help is appreciated!