Adjusting nested dictionary depending on provided key values
<p>I would like to create the following dictionary based on various parameters given by <code>params</code>. Now I do not want to add keys which provide no value. So for instance if <code>params['ean']</code> is blank or does not exist I want to exclude the entire line <code>"EAN": params['ean']</code>. Any suggestions are highly welcome.</p>

<pre><code>myitem = {
"Item": {
     "Title": params['title'],
     "Description": "Some Text",
     "PrimaryCategory": {"CategoryID": params['category_id']},
     "ISBN": params['isbn'],
     "EAN": params['ean']
}
}
</code></pre>

<p>Please note that that params will not have the same keys like myitem.</p>

<p><strong>If you can change the way you create the dict</strong> just user dict comprehension:</p>

<pre><code>myitem = {k: v for k, v in params.items() if v}
</code></pre>

<p>If you already have the dict in first place and you <strong>can't change the initialization</strong> of it, you can delete the empty values like that:</p>

<pre><code>for k, v in myitems.items():
    if not v:
        del myitems[k]
</code></pre>

python dictionary conditional