Return all keys along with value in nested dictionary
Question: I am working on getting all text that exists in several `.yaml` files placed
into a new singular YAML file that will contain the English translations that
someone can then translate into Spanish.

Each YAML file has a lot of nested text. I want to print the full 'path', aka
all the keys, along with the value, for each value in the YAML file. Here's an
example input for a `.yaml` file that lives in the
myproject.section.more_information file:

    
    
    default: 
        heading: Here’s A Title
        learn_more:
            title: Title of Thing
            url: www.url.com
            description: description
            opens_new_window: true
    

and here's the desired output:

    
    
    myproject.section.more_information.default.heading: Here’s a Title
    myproject.section.more_information.default.learn_more.title: Title of Thing
    mproject.section.more_information.default.learn_more.url: www.url.com
    myproject.section.more_information.default.learn_more.description: description
    myproject.section.more_information.default.learn_more.opens_new_window: true
    

This seems like a good candidate for recursion, so I've looked at examples
such as [this answer](http://stackoverflow.com/questions/36808260/python-
recursive-search-of-dict-with-nested-keys?rq=1)

However, I want to preserve all of the keys that lead to a given value, not
just the last key in a value. I'm currently using PyYAML to read/write YAML.

Any tips on how to save each key as I continue to check if the item is a
dictionary and then return all the keys associated with each value?


Answer: What you're wanting to do is flatten nested dictionaries. This would be a good
place to start: [Flatten nested Python dictionaries, compressing
keys](http://stackoverflow.com/questions/6027558/flatten-nested-python-
dictionaries-compressing-keys)

In fact, I think the code snippet in the top answer would work for you if you
just changed the sep argument to `.`.

edit:

Check this for a working example based on the linked SO answer
<http://ideone.com/Sx625B>

    
    
    import collections
    
    some_dict = {
        'default': {
            'heading': 'Here’s A Title',
            'learn_more': {
                'title': 'Title of Thing',
                'url': 'www.url.com',
                'description': 'description',
                'opens_new_window': 'true'
            }
        }
    }
    
    def flatten(d, parent_key='', sep='_'):
        items = []
        for k, v in d.items():
            new_key = parent_key + sep + k if parent_key else k
            if isinstance(v, collections.MutableMapping):
                items.extend(flatten(v, new_key, sep=sep).items())
            else:
                items.append((new_key, v))
        return dict(items)
    
    results = flatten(some_dict, parent_key='', sep='.')
    for item in results:
        print(item + ': ' + results[item])
    

If you want it in order, you'll need an OrderedDict though.

