Count Occurance of Substring in a List of Strings - Python
To count the occurrences of a particular substring in a list of strings in Python, we can use several methods. In this article, we are going to explore different methods to count the existence of a particular substring in a given list.
Using sum() and Generator Expression
This method uses a generator expression inside the sum() function. It's similar to list comprehension but a bit more memory efficient since it doesn't create a new list.
x = ["hello", "world", "hi", "hello world"]
# Use a generator expression to iterate through
# the list and count matches
# For each string (y) in the list (x), check if "hello" is present
count = sum(1 for y in x if "hello" in y)
print(count)
Output
2
- The generator expression (1 for y in x if "hello" in y) generates a 1 for each string in x that contains "hello". Then sum() adds them up to give the total count.
There are multiple other ways to count strings containing a substring in Python.
Table of Content
Using List Comprehension
If we have to make the code shorter and cleaner we can use list comprehension. This method lets us create a list of all strings that contain the substring and then just count how many there are.
x = ["hello", "world", "hi", "hello world"]
# Count the number of strings in the new list using len()
count = len([y for y in x if "hello" in y])
print(count)
Output
2
Using filter() Function
filter() function allows us to filter the list based on a condition. We can filter out the strings that don’t contain the substring and then count the remaining ones.
x = ["hello", "world", "hi", "hello world"]
# Use the filter() function to filter out strings containing "hello"
count = len(list(filter(lambda y: "hello" in y, x)))
print(count)
Output
2
- filter() goes through each string in x and applies the condition lambda y: "hello" in y. It keeps only the strings where "hello" is found.
- We then count the length of the filtered list using len().
Using for loop
Using a for loop is the easiest way to count strings with a substring. We can go through each string in the list and check if the substring is present.
x = ["hello", "world", "hi", "hello world"]
count = 0
# Iterate through each string (y) in the list (x)
for y in x:
if "hello" in y:
count += 1
print(count)
Output
2