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 - Specific Characters Frequency in String List
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'
4 min read
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 ea
3 min read
Python - Maximum frequency character in String
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 modu
3 min read
Split String into List of characters in Python
We are given a string and our task is to split this string into a list of its individual characters, this can happen when we want to analyze or manipulate each character separately. For example, if we have a string like this: 'gfg' then the output will be ['g', 'f', 'g']. Using ListThe simplest way
2 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 - 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
Splitting String to List of Characters - Python
We are given a string, and our task is to split it into a list where each element is an individual character. For example, if the input string is "hello", the output should be ['h', 'e', 'l', 'l', 'o']. Let's discuss various ways to do this in Python. Using list()The simplest way to split a string i
2 min read
Splitting String to List of Characters - Python
The task of splitting a string into a list of characters in Python involves breaking down a string into its individual components, where each character becomes an element in a list. For example, given the string s = "GeeksforGeeks", the task is to split the string, resulting in a list like this: ['G
3 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