Why use regex finditer() rather than findall()
<p>What is the advantage of using <code>finditer()</code> if <code>findall()</code> is good enough?
<code>findall()</code> returns all of the matches while <code>finditer()</code> returns match object which can't be processed as directly as a static list.</p>

<p>For example:</p>

<pre><code>import re
CARRIS_REGEX = (r'&lt;th&gt;(\d+)&lt;/th&gt;&lt;th&gt;([\s\w\.\-]+)&lt;/th&gt;'
                r'&lt;th&gt;(\d+:\d+)&lt;/th&gt;&lt;th&gt;(\d+m)&lt;/th&gt;')
pattern = re.compile(CARRIS_REGEX, re.UNICODE)
mailbody = open("test.txt").read()
for match in pattern.finditer(mailbody):
    print(match)
print()
for match in pattern.findall(mailbody):
    print(match)
</code></pre>

<p>Output:</p>

<pre class="lang-none prettyprint-override"><code>&lt;_sre.SRE_Match object at 0x00A63758&gt;
&lt;_sre.SRE_Match object at 0x00A63F98&gt;
&lt;_sre.SRE_Match object at 0x00A63758&gt;
&lt;_sre.SRE_Match object at 0x00A63F98&gt;
&lt;_sre.SRE_Match object at 0x00A63758&gt;
&lt;_sre.SRE_Match object at 0x00A63F98&gt;
&lt;_sre.SRE_Match object at 0x00A63758&gt;
&lt;_sre.SRE_Match object at 0x00A63F98&gt;

('790', 'PR. REAL', '21:06', '04m')
('758', 'PORTAS BENFICA', '21:10', '09m')
('790', 'PR. REAL', '21:14', '13m')
('758', 'PORTAS BENFICA', '21:21', '19m')
('790', 'PR. REAL', '21:29', '28m')
('758', 'PORTAS BENFICA', '21:38', '36m')
('758', 'SETE RIOS', '21:49', '47m')
('758', 'SETE RIOS', '22:09', '68m')
</code></pre>

<p>I ask this out of curiosity.</p>

<p><code>finditer()</code> returns an iterator while <code>findall()</code> returns an array. An iterator only does work when you ask it to by calling <code>.next()</code>. A for loop knows to call <code>.next()</code> on iterators, meaning if you <code>break</code> from the loop early, any following matches won't be performed. An array, on the other hand, needs to be fully populated, meaning every match must be found up front.</p>

<p>Iterators can be be far more memory and CPU efficient since they only needs to load one item at a time. If you were matching a very large string (encyclopedias can be several hundred megabytes of text), trying to find all matches at once could cause the browser to hang while it searched and potentially run out of memory.</p>

python regex match string-matching iterable