python: dictionaries overwriting when using pythonic way
<p>If I create a dictionary in a pythonic way, it gets overwritten</p>

<pre><code>ans = ['car','bus']
exp = ['a','b']
for ex in exp:
     b ={x: {ex: {'1:N': [],'2:N': []}} for x in ans}
</code></pre>

<p>How do I avoid overwriting of an 'a' key?</p>

<p>It looks like you are beginner in Python so I would suggest break your program into smaller pieces to debug it. 
In your case dictionaries is getting overwritten because <code>=</code> operator always do reassignment that is create new copy and does not modify the existing variable even for mutable data types</p>

<p>If you break your code like this</p>

<pre><code>ans = ['car','bus']
exp = ['a','b']


b = {x:{'1:N': [],'2:N': []} for x in exp} 
print(b)
</code></pre>

<p>It would give<br>
<code>{'b': {'2:N': [], '1:N': []}, 'a': {'2:N': [], '1:N': []}}</code></p>

<p>If you modify dictionary comprehension like this<br>
<code>b = {a:{x:{'1:N': [],'2:N': []} for x in exp} for a in ans}</code></p>

<p>then you would get the output<br>
<code>{'bus': {'b': {'2:N': [], '1:N': []}, 'a': {'2:N': [], '1:N': []}}, 'car': {'b': {'2:N': [], '1:N': []}, 'a': {'2:N': [], '1:N': []}}}</code></p>

<p>I believe this is what you want, let me know if it helps.</p>

python