Python – Count Strings with substring String List
The classical problem that can be handled quite easily by python and has been also dealt with many times is finding if 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.
Python3
# Python code to demonstrate# Count Strings with substring String List# using list comprehension + len()# initializing listtest_list = ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']# printing original listprint ("The original list is : " + str(test_list))# initializing substringsubs = 'Geek'# using list comprehension + len()# Count Strings with substring String Listres = len([i for i in test_list if subs in i])# printing resultprint ("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.
Python3
# Python code to demonstrate# Count Strings with substring String List# using filter() + lambda + len()# initializing listtest_list = ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']# printing original listprint ("The original list is : " + str(test_list))# initializing substringsubs = 'Geek'# using filter() + lambda + len()# Count Strings with substring String Listres = len(list(filter(lambda x: subs in x, test_list)))# printing resultprint ("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





Please Login to comment...