I've seen the answer to this question already Finding a key recursively in a dictionary . However, it does not give me the desired results. The problem is that it stops looking for the given key once it finds it. Meaning that if there are other instances of the key in the dictionary then these will be overlooked.
Let's say my dictionary looks something like the following:
nested_dico = {
'columns': {
'background_color': 'A5300F',
'font': {'bold': True, 'color': 'FFFFFF', 'size': 15}},
'index': {
'background_color': 'E1CDCC'},
'master': {
'align': 'center',
'background_color': 'FFFFFF',
'font': {'color': '000000', 'name': 'arial', 'size': 16},
'vertical_anchor': 'middle'}}
The desired behavior is for a function to be able to apply a given conversion to all elements within the dictionary matching the key.
For example, when running converter(nested_dico, 'size', lambda x: x*2)
I want converter
to return the same dictionary with all the size values multiplied by 2. The actual implementation I need this for is to convert all color values from Hex to RGB code.
Bonus points if you can get it working for partial strings. For example: converter(nested_dico, 'color', RGB())
will change both background_color
and color
values!
Looking ideally for an elegant pythonic solution.