What is the difference between re.findall() and re.finditer() methods available in Python?

This article guides you through the difference between re.findall() and re.finditer() methods available in Python. The re module of Python helps in working with regular expressions.

Both the re.findall() and re.finditer() methods are used to search for patterns, but they behave differently. Let us see the differences with simple explanations and examples in this article.

Using re.findall() Method

The re.findall() method returns a list of all matching patterns found in the string. It searches through the entire string and collects all non-overlapping matches.

Example

Here is an example to show the usage of the findall() method. We find all occurrences of 5-letter words in the given text ?

import re

# Define text here
txt = "apple banana mango"

# Define regex pattern for 5-letter words
pattern = r"\b\w{5}\b"

# Use findall method here
matches = re.findall(pattern, txt)

# Print the result
print("Matches found:", matches)
print("Type:", type(matches))

The output of the above code is ?

Matches found: ['apple', 'mango']
Type: <class 'list'>

Using re.finditer() Method

The re.finditer() method returns an iterator yielding match objects for all non-overlapping matches of a pattern in a string. This method provides more detailed information about each match, such as the start and end positions within the string.

Example

The following example extracts words with exactly five letters and shows their positions ?

import re

txt = "apple banana mango"
pattern = r"\b\w{5}\b"

# Get iterator of match objects
matches = re.finditer(pattern, txt)

print("Match details:")
for match in matches:
    print(f"'{match.group()}' found at position {match.start()}-{match.end()}")

print("Type:", type(re.finditer(pattern, txt)))

The output of the above code is ?

Match details:
'apple' found at position 0-5
'mango' found at position 13-18
Type: <class 'callable_iterator'>

Key Differences

Criteria re.findall() re.finditer()
Return Type List of strings Iterator of match objects
Memory Usage Stores all matches in memory Yields matches one by one
Position Info No position information Provides start/end positions
Best For Simple pattern extraction Detailed match analysis

Practical Comparison

Let's compare both methods side by side with the same pattern ?

import re

text = "The quick brown fox jumps over the lazy dog"
pattern = r"\b\w{4}\b"  # Find 4-letter words

# Using findall()
findall_result = re.findall(pattern, text)
print("findall() result:", findall_result)

# Using finditer()
finditer_result = re.finditer(pattern, text)
print("finditer() details:")
for match in finditer_result:
    print(f"  '{match.group()}' at positions {match.span()}")

The output of the above code is ?

findall() result: ['over', 'lazy']
finditer() details:
  'over' at positions (26, 30)
  'lazy' at positions (35, 39)

Conclusion

Use re.findall() when you need a simple list of matches. Use re.finditer() when you need detailed information about match positions or want memory-efficient processing of large texts.

Updated on: 2026-03-24T19:13:31+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements