The classical problem that can be handled quite easily by python and has been also dealt with many times is finding if a a string is substring of other. But sometimes, one wishes to extend this on list of strings and find how many strings satisfy condition, and hence then requires to traverse the entire container and perform the generic algorithm. Lets discuss certain ways to perform this task.
Method #1 : Using list comprehension + len()
List comprehension is an elegant ways to perform any particular task as it increases readability in a long run. This task can be performed using naive method and hence can be reduced to list comprehension as well. The len() is used to compute length of list.
# Python code to demonstrate # Count Strings with substring String List # using list comprehension + len() # initializing list test_list = ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms'] # printing original list print ("The original list is : " + str(test_list)) # initializing substring subs = 'Geek' # using list comprehension + len() # Count Strings with substring String List res = len([i for i in test_list if subs in i]) # printing result print ("All strings count with given substring are : " + str(res)) |
The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms'] All strings count with given substring are : 2
Method #2 : Using filter() + lambda + len()
This function can also perform this task of finding the strings with the help of lambda. It just filters out all the strings matching the particular substring and then adds it in a new list. The len() is used to compute length of list.
# Python code to demonstrate # Count Strings with substring String List # using filter() + lambda + len() # initializing list test_list = ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms'] # printing original list print ("The original list is : " + str(test_list)) # initializing substring subs = 'Geek' # using filter() + lambda + len() # Count Strings with substring String List res = len(list(filter(lambda x: subs in x, test_list))) # printing result print ("All strings count with given substring are : " + str(res)) |
The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms'] All strings count with given substring are : 2
Recommended Posts:
- Python | Filter list of strings based on the substring list
- Python | Finding strings with given substring in list
- Python | Replace substring in list of strings
- Python | Check if substring is part of List of Strings
- Python | Remove substring list from String
- Python | Substring removal in String list
- Python | Get the substring from given string using list slicing
- Python - Possible Substring count from String
- Python | Count overlapping substring in a given string
- Python | Ways to count number of substring in string
- Python | Splitting string list by strings
- Python | Reverse All Strings in String List
- Python - Filter float strings from String list
- Python | Split strings and digits from string list
- Python | Sort given list of strings by part of string
- Python | Convert list of strings to space separated string
- Python | Count String occurrences in mixed list
- Python | Remove empty strings from list of strings
- Python | Tokenizing strings in list of strings
- Python | Merge list of tuple into list by joining the strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

