Specific Characters Frequency in String List-Python
Last Updated :
01 Mar, 2025
The task of counting the frequency of specific characters in a string list in Python involves determining how often certain characters appear across all strings in a given list.
For example, given a string list ["geeksforgeeks"] and a target list ['e', 'g'], the output would count how many times 'e' and 'g' appear across all the strings in the list. The output in this case would be {'g': 2, 'e': 4}, indicating that 'g' appears twice and 'e' appears four times across all the strings in the list.
Using collections.Counter
Counter from the collections module is a convenient tool for counting occurrences of elements in an iterable. By converting a string list into a single string and applying Counter, we can quickly count the frequency of all characters. To focus on specific characters, a dictionary comprehension is used to filter only those characters present in the target list.
Python
from collections import Counter
# test list
a = ["geeksforgeeks is best for geeks"]
# char list
b = ['e', 'b', 'g']
# join the list into a single string and use Counter
res = {key: val for key, val in Counter("".join(a)).items() if key in b}
print(res)
Output{'g': 3, 'e': 7, 'b': 1}
Explanation: This code combines the list a into a string, counts the frequency of each character using Counter() and filters the result to include only the characters found in list b, storing them in a dictionary.
Using dictionary
Manually iterating through each character in the list and counting its occurrences gives us more control over the process. A dictionary is used to track the frequency of characters in the string. By checking if each character is in the target list, we update the count accordingly.
Python
# test list
a = ["geeksforgeeks is best for geeks"]
# char list
b = ['e', 'b', 'g']
# manually count the frequency of characters in the list
char_count = {}
for s in a:
for c in s:
if c in b:
char_count[c] = char_count.get(c, 0) + 1
print(char_count)
Output{'g': 3, 'e': 7, 'b': 1}
Explanation: This code iterates through each string in list a and each character in the string. For each character, it checks if it is in list b. If it is, the character's count is updated in the char_count dictionary using get() to handle missing keys.
Using filter()
filter() function is used to remove characters from the string that are not part of the target list. After filtering, Counter is applied to count the occurrences of the remaining characters. This method combines functional programming with the ease of counting using Counter.
Python
from collections import Counter
# test list
a = ["geeksforgeeks is best for geeks"]
# char list
b = ['e', 'b', 'g']
# filter the string and count using Counter
filtered_str = "".join(filter(lambda x: x in b, "".join(a)))
res = dict(Counter(filtered_str))
print(res)
Output{'g': 3, 'e': 7, 'b': 1}
Explanation: "".join(a) combines the list a into a string, then filter() with a lambda retains characters from b. The filtered characters are rejoined and Counter(filtered_str) counts their occurrences, converting the result into a dictionary with dict().
Using dictionary comprehension
List comprehension offers a more Pythonic way to count the frequency of specific characters in a string. By first creating a list of only the characters that are in the target list, Counter is then used to count the occurrences of each character. This approach is compact and efficient for solving the problem in a single line.
Python
from collections import Counter
# test list
a = ["geeksforgeeks is best for geeks"]
# char list
b = ['e', 'b', 'g']
# list comprehension to count the characters
res = {key: val for key, val in Counter([char for char in "".join(a) if char in b]).items()}
print(res)
Output{'g': 3, 'e': 7, 'b': 1}
Explanation: This code combines list a into a string, filters characters from b using a dictionary comprehension, counts their frequencies with Counter(), and converts the result into a dictionary.
Similar Reads
Python - Least Frequent Character in String The task is to find the least frequent character in a string, we count how many times each character appears and pick the one with the lowest count.Using collections.CounterThe most efficient way to do this is by using collections.Counter which counts character frequencies in one go and makes it eas
3 min read
Python - Sort String list by K character frequency Given String list, perform sort operation on basis of frequency of particular character. Input : test_list = ["geekforgeekss", "is", "bessst", "for", "geeks"], K = 's' Output : ['bessst', 'geekforgeekss', 'geeks', 'is', 'for'] Explanation : bessst has 3 occurrence, geeksforgeekss has 3, and so on. I
4 min read
Maximum Frequency Character in String - Python The task of finding the maximum frequency character in a string involves identifying the character that appears the most number of times. For example, in the string "hello world", the character 'l' appears the most frequently (3 times).Using collection.CounterCounter class from the collections modul
3 min read
Python - Successive Characters Frequency Sometimes, while working with Python strings, we can have a problem in which we need to find the frequency of next character of a particular word in string. This is quite unique problem and has the potential for application in day-day programming and web development. Let's discuss certain ways in wh
6 min read
Python - Sort Strings by maximum frequency character Given a string, the task is to write a Python program to perform sort by maximum occurring character. Input : test_list = ["geekforgeeks", "bettered", "for", "geeks"] Output : ['for', 'geeks', 'bettered', 'geekforgeeks'] Explanation : 1 < 2 < 3 < 4, is ordering of maximum character occurren
3 min read
Python - Expand Character Frequency String Given a string, which characters followed by its frequency, create the appropriate string. Examples: Input : test_str = 'g7f2g3i2s2b3e4' Output : gggggggffgggiissbbbeeee Explanation : g is succeeded by 7 and repeated 7 times. Input : test_str = 'g1f1g1' Output : gfg Explanation : f is succeeded by 1
4 min read
Python - Bigrams Frequency in String Sometimes while working with Python Data, we can have problem in which we need to extract bigrams from string. This has application in NLP domains. But sometimes, we need to compute the frequency of unique bigram for data collection. The solution to this problem can be useful. Lets discuss certain w
4 min read
Python - List Words Frequency in String Given a List of Words, Map frequency of each to occurrence in String. Input : test_str = 'geeksforgeeks is best for geeks and best for CS', count_list = ['best', 'geeksforgeeks', 'computer'] Output : [2, 1, 0] Explanation : best has 2 occ., geeksforgeeks 1 and computer is not present in string.Input
4 min read
Prefix frequency in string List - Python In this article, we will explore various methods to find prefix frequency in string List. The simplest way to do is by using a loop.Using a LoopOne of the simplest ways to calculate the frequency of a prefix in a list of strings is by iterating through each element and checking if the string starts
2 min read
Python - Frequency of K in sliced String Given a String, find the frequency of certain characters in the index range. Input : test_str = 'geeksforgeeks is best for geeks', i = 3, j = 9, K = 'e' Output : 0 Explanation : No occurrence of 'e' between 4th [s] and 9th element Input : test_str = 'geeksforgeeks is best for geeks', i = 0, j = 9, K
6 min read