How to convert to Python list comprehension
<pre><code>ls = ['abc', 56, 49, 63, 66, 80]
for i in ls:
    if(isinstance(i, int) or isinstance(i, float)):
        for i in range(len(ls)):
            ls[i] = str(ls[i])
</code></pre>

<p>May I know how to create the list comprehension of above code?</p>

<p>I am trying  the following but not working</p>

<pre><code>if (s for s in ls isinstance(s, int) or isinstance(s, float)):
    for i in range(len(ls)):
        ls[i] = str(ls[i])
</code></pre>

<p>For your example where you have either strings or integers or floats, you can use a simple list comprehension:</p>

<pre><code>ls = ['abc', 56, 49, 63, 66, 80]
print([str(l) for l in ls])
</code></pre>

<p>Or alternatively, you can use <a href="https://docs.python.org/3/library/functions.html#map"><code>map</code></a>:</p>

<pre><code>print(list(map(str, ls)))
</code></pre>

<p>If you only want to convert floats and integers to strings (and ignore anything else like booleans, decimal objects, etc.):</p>

<pre><code>print([str(l) if isinstance(l, int) or isinstance(l, float) else l for l in ls])
</code></pre>

python list python-3.x for-comprehension