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 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']
All strings count with given substring are : 2



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.