Efficiency of for loops in python3
<p>I am currently learning Python (3), having mostly experience with R as main programming language. While in R <code>for</code>-loops have mostly the same functionality as in Python, I was taught to avoid using it for big operations and instead use <code>apply</code>, which is more efficient.</p>

<p>My question is: how efficient are <code>for</code>-loops in Python, are there alternatives and is it worth exploring those possibilities as a Python newbie? </p>

<p>For example: </p>

<pre><code>p = some_candidate_parameter_generator(data)
for i in p:
    fit_model_with paramter(data, i)
</code></pre>

<p>Bare with me, it is tricky to give an example without going too much into specific code. But this is something that in <code>R</code> I would have writting with <code>apply</code>, especially if <code>p</code> is large.</p>

<p>The comments correctly point out that for loops are "only as efficient as your logic"; however, the <code>range</code> and <code>xrange</code> in Python do have performance implications, and this may be what you had in mind when asking this question. <em>These methods have nothing to do with the intrinsic performance of for loops though.</em></p>

<p>In Python 3.0, <code>xrange</code> is now implicitly just <code>range</code>; however, in Python versions less than 3.0, there used to be a distinction â <code>range</code> loaded your entire iterable into memory, and then iterated over each item, while <code>xrange</code> was more akin to a generator, where each item was loaded into memory only when needed and then removed from memory after it was iterated over.</p>

<p><strong>After your updated question:</strong></p>

<p>In other words, if you have a giant list of items that you need to iterate over via a for loop, it is often more memory efficient to use a generator, not a list or a tuple, etc. Again though, this has nothing to do with how the Python for-loop operates, but more to do with what you're iterating over. If in doubt, use a generator, and your memory-efficiency will be as good as it will get with Python.</p>

python