For example:
I need to create a dict of dicts when iterating over a list of keys
For example, I have this list
my_dict = {'root': {}}
my_keys = ['foo', 'bar', 'lorem', 'ipsum']
I wanna create a function to return this following dict
my_dict = {
'root': {
'foo': {
'bar': {
'lorem': {
'ipsum': {}
}
}
}
}
}
I think something using recursion, but I'm stucked in the logic.
With dict.setdefault
function:
curr_d = my_dict['root']
for k in my_keys:
curr_d = curr_d.setdefault(k, {})