This is not an answer about using .extend()
.
Your specification says find all substrings, but only uses a test for a single character. The following code will handle longer substrings, and do it with a single pass using regular expressions. This is a significant performance speed up as the number of substrings increases. Regular expressions are our friends!
import re
def find_all_substrings(target, substrings):
# regex to scan for all substrings in one mass
pattern = "|".join(map(re.escape, substrings))
# Scan once for all strings
matches = re.finditer(pattern, target)
# Get start position and matched string
results = [(match.start(),match.group()) for match in matches]
return results
print(find_all_substrings("I have an apple, a banana, and a cherry in my bag. Also, another apple.", ['apple', 'banana', 'cherry']))
test_string = "abcdefg"
look_for = ["b", "f"]
print(find_all_substrings(test_string, look_for))
giving the following output:
[(10, 'apple'), (19, 'banana'), (33, 'cherry'), (65, 'apple')]
[(1, 'b'), (5, 'f')]
If you are determined to use a call to .extend(), possibly to allow modifying the results on the fly, you could use:
import re
def find_all_substrings_generator(target, substrings):
# REGEX to scan for all substrings in one mass
pattern = "|".join(map(re.escape, substrings))
matches = re.finditer(pattern, target)
# Get start position and matched string
for match in matches:
yield (match.start(), match.group())
results = []
test_string = "abcdefg"
look_for = ["b", "f"]
for i, result in enumerate(find_all_substrings_generator(test_string, look_for)):
print(f"Result {i}: {result}")
results.extend([result])
print(results)
With the output:
Result 0: (1, 'b')
Result 1: (5, 'f')
[(1, 'b'), (5, 'f')]