1

I have a tree structure, builded from a nested dictionary :

{
  'test': {
    'Data': {},
  },
  'Test': {
    'data': {
      'Other': {},
    },
  },
}

And I want transform it to :

{
  'test': {
    'data': {
      'other': {},
    },
  },
}

Is there any way to perform this transformation in python ?

I insist : all value are dictionary.

linkdd
  • 977
  • 1
  • 9
  • 24

1 Answers1

4

Try a recursive function call to lowercase the keys:

>>> def lower_keys(tree):
        if not tree:
            return tree
        return {k.lower() : lower_keys(v) for k, v in tree.items()}

>>> t = {
  'test': {
    'Data': {},
  },
  'Test': {
    'data': {
      'Other': {},
    },
  },
}

>>> lower_keys(t)
{'test': {'data': {'other': {}}}}
Raymond Hettinger
  • 199,887
  • 59
  • 344
  • 454