In Python how can I copy a list of dictionaries, and filter specific keys, including nested ones?
<p>If I have a dictionary, which contains lists, dictionaries, strings, and integers, how can I copy that dictionary, but only specific white listed keys?</p>

<p>Filtering <code>old_dict</code> through this function would give me <code>new_dict</code>. The white listed keys can be in whatever format, but the solution should be able to take an object of arbitrary white listed keys to produce the output.</p>

<pre><code>old_dict = [
    {
        'name': {'first': "John", 'last': "Doe"},
        'groups': ["foo", "bar"],
        'widgets': [
            {'id': 0, 'name': "Acme"},
            {'id': 1, 'name': "Anvil"},
        ],
    },

    {
        'name': {'first': "David"},
        'groups': ["bar", "bash", "ding"],
        'widgets': [
            {'id': 1, 'name': "Anvil"},
            {'id': 8, 'name': "Bingo"},
        ],
    },
]

new_dict = [
    {
        'name': {'last': "Doe"},
        'widgets': [
            {'name': "Acme"},
            {'name': "Anvil"},
        ],
    },

    {
        'name': { },
        'widgets': [
            {'name': "Anvil"},
            {'name': "Bingo"},
        ],
    },
]
</code></pre>
