Performance comparison between type casting procedures
<p>I am reading lines from a file and want to convert them to a integer list.The file has one million lines.So,I was just wondering which procedure will make it faster.say,
<code>lst=['10','12','31','41','15']</code> is the current line which has been read from the file.
I can do the casting using map function,like-
<code>lst1=map(int,lst)</code>
or using the for function,like-
<code>lst2=[int(x) for x in lst]</code>
Which one would be the most efficient  and the fastest way to do it?</p>

<p><code>[int(x, 10) for x in lst]</code> is at least significantly faster than your two ways:</p>

<pre><code>&gt;&gt;&gt; from timeit import timeit
&gt;&gt;&gt; setup = "lst = ['10','12','31','41','15']"

&gt;&gt;&gt; timeit('map(int, lst)', setup)
4.454881024529442

&gt;&gt;&gt; timeit('[int(x) for x in lst]', setup)
4.7153410495946275

&gt;&gt;&gt; timeit('[int(x, 10) for x in lst]', setup)
1.6508196962672343
</code></pre>

<p>Tested with Python 2.7.11 (I'm assuming you use Python 2 because otherwise your <code>lst1=map(...)</code> would be misleading).</p>

python casting