[Tutor] Fwd: findall() returns tuples or string?
Devin Jeanpierre
jeanpierreda at gmail.com
Tue Mar 5 02:47:12 CET 2013
On Mon, Mar 4, 2013 at 8:22 PM, Charles Leviton
<charles.leviton at gmail.com> wrote:
> I have some confusion regarding when findall returns a list of strings and
> when it returns a list of tuples.
> Would appreciate an explanation.
If there are no groups, it returns a list of strings, where the
strings are the whole match.
If there is one group, it returns a list of strings, where the strings
are the content of the group.
Otherwise, it returns a list of tuples of strings, where the tuples
contain the group contents (the same as if an individual match had its
groups() method called).
If you want consistency, use finditer instead of findall.
http://docs.python.org/2/library/re.html#re.finditer
-- Devin
More information about the Tutor
mailing list