
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between re.findall() and re.finditer() methods available in Python?
This article gives a guide on 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() helps to get a list of all matching patterns. It searches from the start or end of the given string. If we use the findall() method to search for a pattern in a given string, it will return all occurrences of the pattern.
Example
Here is an example to show the usage of the findall() method. Here we find all occurrences of a pattern in the given text and print the returned list.
# Import re module import re # define text here txt = "apple banana mango" # define regex pattern here pattern = r"\b\w{5}\b" # use findall method here matches = re.findall(pattern, txt) # print the result print(matches)
Output
This will create the below outcome ?
['apple', 'mango']
Using re.finditer() Method
The Python 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. Each match can be used to extract matched substrings and their positions.
Example
The following example will extract words with exactly five letters from a given text. We have used a regex pattern that matches words (\w{5}) with a word boundary (\b) to ensure only complete words are selected.
import re txt = "apple banana mango" pattern = r"\b\w{5}\b" matches = re.finditer(pattern, txt) for m in matches: print(m.group(), "starts at", m.start())
Output
This will generate the following result -
apple starts at 0 mango starts at 13
Difference between re.findall() and re.finditer()
Here is the tabular difference between re.findall() and re.finditer() functions -
Criteria | re.findall() | re.finditer() |
---|---|---|
What it returns | This function returns a list of all matches. | This function returns each match one by one as an iterator. |
Type of result | The type of the result is simple text as a list. | The type of the result is special match objects with extra details. |
Memory usage | This method stores all matches at once, using more memory. | This method finds matches one by one, using less memory. |
Best for | This method is best for getting all results quickly. | This method is best for going through each match in detail. |