Unsupported operand type for - list and list
<p>I am using an older version of python which doesnt have support to subtract dict's.</p>

<pre><code>d1={'ab': ['3'], 'hij': ['1200']}
d2={'ab': ['1'], 'hij': ['600']}
</code></pre>

<p>Basically my code looks like this:</p>

<pre><code>  for key in d1.keys():
        if key in d2:
            d3[key]=d1[key]-d2[key]
</code></pre>

<p>output should be like</p>

<pre><code>d2={'ab': ['2'], 'hij': ['600']}
</code></pre>

<p>It returns unsupported operand type for - : list and list.
Any ideas on how to get around this</p>

<p>You can't subtract lists like so. </p>

<p>You could instead use a <em>dictionary comprehensio</em>n and then <code>zip</code> the lists so you can then subtract their content:</p>

<pre><code>d1={'ab': ['3'], 'hij': ['1200']}
d2={'ab': ['1'], 'hij': ['600']}

d3 = {k: ['{}'.format(int(i) - int(j)) for i, j in zip(d1[k], d2[k])] for k in d1}
print(d3)
# {'hij': ['600'], 'ab': ['2']}
</code></pre>

<p>Notice the items where casted to <code>int</code> and then back to string using string formatting. Might be better if your data was actually stored as integers in the first place.</p>

python python-2.7