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>I guess this is what you want.</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'}]
for datadict in clean_dict:
    for key, value in datadict.items():
        if value == '*':
            datadict[key] = 4
        elif value == '':
            datadict[key] = 0
        else:
            try:
                datadict[key] = int(value)
            except:
                continue
</code></pre>

<p>Changes explained:</p>

<ul>
<li><code>int(4)</code> or <code>int(0)</code> is unneccessary. </li>
<li><code>value == int(value)</code> does nothing, i.e, it doesn't update your list or dictionary.</li>
<li><code>for name, datalist in clean_data.iteritems():</code> name is unused and also this does not help to update your list.</li>
<li><code>try</code> and <code>except:</code> is used because string value such as <code>Australia</code> cannot be converted to <code>int</code> type.</li>
</ul>

python list dictionary replace