Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - iterate and update a nested dictionary & lists

Having the following dict, where some of the values can be list of dictionaries:

{
  "A": [
    {
      "B": {
        "C": "D",
        "X": "CHNAGE ME"
      }
    },
    {
      "E": "F"
    }
  ],
  "G": {
    "Y": "CHANGE ME"
  }
}

I would like to recursively iterate over the items and change the pairs of key values where the value is "CHANGE ME", so the result would be:

{
  "A": [
    {
      "B": {
        "C": "D",
        "X.CHANGED": "CHANGED"
      }
    },
    {
      "E": "F"
    }
  ],
  "G": {
    "Y.CHANGED": "CHANGED"
  }
}

Solutions I've found were not handling a case where the value is a list, for example:

import collections
def nested_dict_iter(nested):
    for key, value in nested.iteritems():
        if isinstance(value, collections.Mapping):
            for inner_key, inner_value in nested_dict_iter(value):
                yield inner_key, inner_value
        else:
            yield key, value

How can I achieve my goal?

like image 981
Shlomi Schwartz Avatar asked Nov 10 '20 09:11

Shlomi Schwartz


Video Answer


1 Answers

Using recursion

Ex:

def update(data):
    for k, v in data.copy().items():
        if isinstance(v, dict):     # For DICT
            data[k] = update(v)
        elif isinstance(v, list):   # For LIST
            data[k] = [update(i) for i in v]
        elif v == 'CHANGE ME':      # Update Key-Value
            # data.pop(k)
            # OR
            del data[k]
            data[f"{k}.CHANGED"] = 'CHANGED'
    
    return data

print(update(data))

Output:

{
    'A':[{'B': {'C': 'D', 'X.CHANGED': 'CHANGED'}}, {'E': 'F'}], 
    'G':{'Y.CHANGED': 'CHANGED'}
 }

Note: I have not tested all corner cases

like image 55
Rakesh Avatar answered Sep 19 '22 06:09

Rakesh