Here is the dictionary:
data = {
'a': {
'b': {
'c': {
'd': {
'e': {
'f': 1,
'g': 50,
'h': [1, 2, 4],
'i': 3,
'j': [7, 9, 6],
'k': [
[('x', 'abc')],
[('y', 'qwe')],
[('z', 'zxc')]
]
}
}
}
}
}
}
My goal is to find and convert values to dictionaries, if possible:
data = {
'a': {
'b': {
'c': {
'd': {
'e': {
'f': 1,
'g': 50,
'h': [1, 2, 4],
'i': 3,
'j': [7, 9, 6],
'k': [{
'x': 'abc'
}, {
'y': 'qwe'
}, {
'z': 'zxc'
}]
}
}
}
}
}
}
I think this can be done using recursion, and I even wrote one, but it does not work.
def f(d):
for key, value in d.iteritems():
if type(d[key]) is dict:
f(d)
try:
d[key] = dict(d[key])
except:
if type(d[key]) is list:
for i in d[key]:
try:
d[key][i] = dict(d[key][i])
except:
pass
return d
Error:
RecursionError: maximum recursion depth exceeded when calling Python object
How can I make it work?
If you could provide a solution without recursion, I would also be happy to receive one.