The most efficient way to iterate over a list of elements. Python 2.7
<p>I am trying to iterate over a list of elements, however the list can be massive and takes too long to execute. I am using newspaper api. The for loop I constructed is:</p>

<pre><code>for article in list_articles:
</code></pre>

<p>Each article in the list_articles are an object in the format of:</p>

<pre><code>&lt;newspaper.article.Article object at 0x1103e1250&gt;
</code></pre>

<p>I checked that some recommended using xrange or range, however that did not work in my case, giving a type error:</p>

<pre><code>TypeError: 'int' object is not iterable
</code></pre>

<p>It would be awesome if anyone can point me to the right direction or give me some idea that can efficietly increase iterating over this list.</p>

<p>Here's a little benchmark to make the question a little bit more interesting:</p>

<pre><code>import timeit
import random

N = 1000000


class Foo:

    def __init__(self):
        self.n = random.randint(0, 1000)

bar = [Foo() for r in xrange(N)]


def f1(lst):
    return [v for v in lst]


def f2(lst):
    return [v for index, v in enumerate(lst)]


def f3(lst):
    return [lst[i] for i in range(len(lst))]

K = 100
print timeit.timeit('f1(bar)', setup='from __main__ import f1, bar', number=K)
print timeit.timeit('f2(bar)', setup='from __main__ import f2, bar', number=K)
print timeit.timeit('f3(bar)', setup='from __main__ import f3, bar', number=K)
</code></pre>

<p>In my machine gives:</p>

<pre><code>5.1150355947
6.89524135475
7.90469366922 
[Finished in 22.1s]
</code></pre>

<p>Conclusion: Iterating using the idiom <code>for v in huge_list</code> is a really good choice, so you should profile to check where the bottleneck is, which is probably in the inner loop (items processing)</p>

python list python-2.7 loops cpython