Map if it can be converted
<p>I have the following list:</p>

<pre><code>a = ['1', '2', 'hello']
</code></pre>

<p>And I want to obtain</p>

<pre><code>a = [1, 2, 'hello']
</code></pre>

<p>I mean, convert all integers I can.</p>

<p>This is my function:</p>

<pre><code>def listToInt(l):
    casted = []
    for e in l:
        try:
            casted.append(int(e))
        except:
            casted.append(e)
    return casted
</code></pre>

<p>But, can I use the <code>map()</code> function or something similar?</p>

<p>Sure you can do this with <code>map</code></p>

<pre><code>def func(i):
    try:
        i = int(i)
    except:
        pass
    return i
a = ['1', '2', 'hello']
print(list(map(func, a)))
</code></pre>

python algorithm list casting