Python – Prefix frequency in string List
Sometimes, while working with Python lists, we can have a problem in which we need to get the count of strings that start with a particular substring. This can have an application in web development and general programming. Let us discuss certain ways in which this task can be performed.
Method #1 : Using loop + startswith()
The combination of the above functions can be used to perform this task. In this, we run a loop for each string in list and employ startswith() to get the strings that start with a particular prefix.
Python3
# Python3 code to demonstrate# Prefix frequency in List# using loop + startswith()# Initializing listtest_list = ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses']# printing original listprint("The original list is : " + str(test_list))# Initializing substringtest_sub = 'gfg'# Prefix frequency in List# using loop + startswith()res = 0for ele in test_list: if ele.startswith(test_sub): res = res + 1# printing resultprint("Strings count with matching frequency : " + str(res)) |
The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] Strings count with matching frequency : 3
Method #2: Using sum() + startswith()
The combination of above functions can be used to perform this task. In this, we perform the task of counting using sum() and startswith(), is used to perform task of checking of prefix.
Python3
# Python3 code to demonstrate# Prefix frequency in List# using sum() + startswith()# Initializing listtest_list = ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses']# printing original listprint("The original list is : " + str(test_list))# Initializing substringtest_sub = 'gfg'# Prefix frequency in List# using sum() + startswith()res = sum(sub.startswith(test_sub) for sub in test_list)# printing resultprint("Strings count with matching frequency : " + str(res)) |
The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] Strings count with matching frequency : 3
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(1) constant additional space is created
Method #3 : Using find() method
Python3
# Python3 code to demonstrate# Prefix frequency in List# Initializing listtest_list = ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses']# printing original listprint("The original list is : " + str(test_list))# Initializing substringtest_sub = 'gfg'# Prefix frequency in List# using loop + find()res = 0for ele in test_list: if ele.find(test_sub)==0: res = res + 1 # printing resultprint ("Strings count with matching frequency : " + str(res)) |
The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] Strings count with matching frequency : 3
Method #4 : Using re
Another approach to solve this problem is using the re (regular expression) module in Python. This can be done using the re.match function to match the regular expression pattern to the start of the string. Here is an example:
Python3
import re# Initializing listtest_list = ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses']# printing original listprint("The original list is : " + str(test_list))# Initializing substringtest_sub = 'gfg'# Prefix frequency in List using re.matchres = 0for ele in test_list: if re.match(f'^{test_sub}', ele): res += 1# printing resultprint("Strings count with matching frequency : " + str(res))#This code is contributed by Edula Vinay Kumar Reddy |
The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] Strings count with matching frequency : 3
Time complexity: O(n * m), where n is the length of the list and m is the length of the prefix.
Auxiliary Space: O(1)
Method #5: Using reduce() and lambda function:
In this method, we are using the reduce() function to apply a lambda function to each element of the list. The lambda function checks if the element starts with the given substring, and if so, it increments the count by 1 else no change.
Python3
from functools import reduce# Initializing listtest_list = ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses']# Initializing substringtest_sub = 'gfg'# using lambda function to check if a string starts with the given substringcount_func = lambda count, string: count + 1 if string.startswith(test_sub) else count# using reduce() function to apply the lambda function to each element of the listres = reduce(count_func, test_list, 0)# printing original listprint("The original list is : " + str(test_list))# printing resultprint("Strings count with matching frequency : " + str(res)) |
The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] Strings count with matching frequency : 3
Time complexity: O(n * m) where n is the length of the list and m is the length of the prefix.
Auxiliary Space: O(1)
Method #6: Using the filter() method
Step by step Algorithm:
- Initialize a list of strings and a substring to find in the list.
- Use the filter() method to create a new iterator that contains only the elements in the list that start with the substring.
- Use the len() method to count the number of elements in the iterator.
- Print the original list of strings and the number of strings in the list that start with the substring.
Python3
# Initialize a list of stringstest_list = ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses']# Initialize a substring to find in the listtest_sub = 'gfg'# Create a new iterator that contains only the elements in the list that start with the substringnew_iter = filter(lambda element: element.startswith(test_sub), test_list)# Count the number of elements in the iterator using the len() methodcount = len(list(new_iter))# Print the original list of stringsprint("The original list is : " + str(test_list))# Print the number of strings in the list that start with the substringprint("Strings count with matching frequency: ", count) |
The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] Strings count with matching frequency: 3
Time complexity: O(n) where n is the length of the list.
Auxiliary Space: O(k) where k is the number of elements in the list that start with the given substring.


Please Login to comment...