Skip to content

Nested key-value dict recursive

to avoid my_dict.get('key').get('key').get('key'), in this example, it will be: my_dict.get_data('key.key.key')

python
    def get_data(self, value):
        return self._aaa(self._data, value.split('.'))

    def _aaa(self, dictionary, keywords):

        if not dictionary:
            return None

        if not keywords:
            return dictionary

        if len(keywords) == 1:
            return dictionary.get(keywords[0])
        return self._aaa(dictionary.get(keywords[0]), keywords[1:])