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 particular substring. This can have an application in web development and general programming. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop + startswith()
The combination of 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 particular prefix.
# Python3 code to demonstrate # Prefix frequency in List # using loop + startswith() # Initializing list test_list = ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] # printing original list print("The original list is : " + str(test_list)) # Initializing substring test_sub = 'gfg' # Prefix frequency in List # using loop + startswith() res = 0for ele in test_list: if ele.startswith(test_sub): res = res + 1 # printing result print ("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 code to demonstrate # Prefix frequency in List # using sum() + startswith() # Initializing list test_list = ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] # printing original list print("The original list is : " + str(test_list)) # Initializing substring test_sub = 'gfg' # Prefix frequency in List # using sum() + startswith() res = sum(sub.startswith(test_sub) for sub in test_list) # printing result print ("Strings count with matching frequency : " + str(res)) |
The original list is : [‘gfgisbest’, ‘geeks’, ‘gfgfreak’, ‘gfgCS’, ‘Gcourses’]
Strings count with matching frequency : 3
Recommended Posts:
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Python | Prefix sum list
- Python | Product of Prefix in list
- Python | Remove prefix strings from list
- Python | Split strings in list with same prefix in all elements
- Python | Append suffix/prefix to strings in list
- Python - Filter list elements starting with given Prefix
- Python | Get the numeric prefix of given string
- Python | Get numeric prefix of given string
- Python | List frequency of elements
- Python | Finding frequency in list of tuples
- Python | Element with largest frequency in list
- Python - List Strings frequency in Matrix
- Python | Frequency grouping of list elements
- Python - Step Frequency of elements in List
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.

