I have a string: candidate__name__first_name. I want to convert it into a nested dictionary of the form:
{
candidate: {
name: {
first_name: 'MyName'
}
}
}
What would be the best way to do this?
How about this:
recursive_key = 'candidate__name__first_name'
value = 'MyName'
for key in reversed(recursive_key.split('__')):
value = {key: value}
print(value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With