replace values in list of python dictionaries
<p>So I have a huge list of dictionaries and I want to: </p>

<ul>
<li>change asterixs into the integer "4"</li>
<li>change all spaces "" to the integer "0"</li>
<li>change all other numbers to integers</li>
</ul>

<p>(this is only a portion of the list)</p>

<pre><code> clean_dict= [{'origin': 'Various/Unknown', 'idps': '', 'year': '1951', 'total': '180000', 'stateless': '', 'ret_refugees': '', 'asylum': '', 'country': 'Australia', 'others': '', 'ret_idps': '', 'refugees': '180000'}, {'origin': 'Various/Unknown', 'idps': '', 'year': '1951', 'total': '282000', 'stateless': '', 'ret_refugees': '', 'asylum': '', 'country': 'Austria', 'others': '', 'ret_idps': '', 'refugees': '282000'}]
</code></pre>

<p>I tried this code but nothing happened, any help is very appreciated! </p>

<p>Also <code>pandas</code> library is not allowed for this assignment.</p>

<pre><code> for name, datalist in clean_data.iteritems():
    for datadict in datalist:
        for key, value in datadict.items():
            if value == "*":
                datadict[key] = int(4)
            elif value == "":
                datadict[key]= int(0)
            else:
                value == int(value)
</code></pre>

<p>Assuming you're using Python 3 try this. For Python 2.x the main difference is to use <code>iteritems()</code> instead of <code>items()</code> but the rest should remain the same.</p>

<pre><code>for dict in clean_dict:
    for k,v in dict.items():
        if v == '*':
            dict[k] = 4
        elif v == '':
            dict[k]= 0
        else:
            # not necessarily an integer so handle exception
            try:
                dict[k] = int(v)
            except ValueError:
                pass
</code></pre>

python list dictionary replace