Count Occurance of Substring in a List of Strings - Python Last Updated : 28 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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 ExpressionThis 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. Python 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) Output2 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 ContentUsing List ComprehensionUsing filter() FunctionUsing for loopUsing List ComprehensionIf 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. Python 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) Output2 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. Python 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) Output2 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 loopUsing 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. Python 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) Output2 Comment More infoAdvertise with us Next Article Count Occurance of Substring in a List of Strings - Python M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Practice Tags : python Similar Reads Python - All occurrences of Substring from the list of strings Given a list of strings and a list of substring. The task is to extract all the occurrences of a substring from the list of strings. Examples: Input : test_list = ["gfg is best", "gfg is good for CS", "gfg is recommended for CS"] subs_list = ["gfg", "CS"] Output : ['gfg is good for CS', 'gfg is reco 5 min read Python - All occurrences of substring in string A substring is a contiguous occurrence of characters within a string. Identifying all instances of a substring is important for verifying various tasks. In this article, we will check all occurrences of a substring in String.Using re.finditer()re.finditer() returns an iterator yielding match objects 3 min read Create List of Substrings from List of Strings in Python In Python, when we work with lists of words or phrases, we often need to break them into smaller pieces, called substrings. A substring is a contiguous sequence of characters within a string. Creating a new list of substrings from a list of strings can be a common task in various applications. In th 3 min read Extract List of Substrings in List of Strings in Python Working with strings is a fundamental aspect of programming, and Python provides a plethora of methods to manipulate and extract substrings efficiently. When dealing with a list of strings, extracting specific substrings can be a common requirement. In this article, we will explore five simple and c 3 min read Python | Count overlapping substring in a given string Given a string and a sub-string, the task is to get the count of overlapping substring from the given string. Note that in Python, the count() function returns the number of substrings in a given string, but it does not give correct results when two occurrences of the substring overlap. Consider thi 2 min read Like