Python copy parts of dictionary into a new dictionary
<p>I have a python dictionary which looks like this:</p>

<pre><code>old_dict={"payment_amt": "20",
          "chk_nr": "321749",
          "clm_list": {"dtl": [{"clm_id": "1A2345", "name": "John"},
                               {"clm_id": "9999", "name": "Jack"}]}}
</code></pre>

<p>I need to parse the above and store it as:</p>

<pre><code>{"payment_amt": "20",
 "clm_list": {"dtl": [{"clm_id": "1A2345"},
                      {"clm_id": "9999"}]}}
</code></pre>

<p>Is there a right pythonic way to do it?</p>

<p>Thanks</p>

<p>Yes, there are many "<em>right pythonic ways to do it</em>".
Here is one such way:</p>

<pre><code>old_dict = {
    "payment_amt": "20",
    "chk_nr": "321749",
    "clm_list": {
        "dtl": [
            {"clm_id": "1A2345", "name": "John"},
            {"clm_id": "9999", "name": "Jack"}]}}

new_dict = {
    'payment_amt': old_dict['payment_amt'],
    'clm_list': {
        'dtl': [{
            'clm_id': dtl['clm_id']} for dtl in old_dict['clm_list']['dtl']]}}

assert new_dict == {
    "payment_amt": "20",
    "clm_list": {"dtl": [{"clm_id": "1A2345"}, {"clm_id": "9999"}]}}
</code></pre>

python dictionary