Python – Case Insensitive string counter
Last Updated :
23 Apr, 2023
Given a list of strings, find the frequency of strings case insensitive.
Input : test_list = ["Gfg", "Best", "GFG", "is", "IS", "BEST"]
Output : {'gfg': 2, 'best': 2, 'is': 2}
Explanation : All occur twice.
Input : test_list = ["Gfg", "gfg", "GFG"]
Output : {'gfg': 3}
Explanation : Only "gfg" 3 occurrences.
Method 1: Using defaultdict() + lower()
In this, we perform lower() to all the strings, before mapping in defaultdict. This ensures case insensitivity while mapping and cumulating frequency.
Python3
from collections import defaultdict
test_list = [ "Gfg" , "Best" , "best" , "gfg" , "GFG" , "is" , "IS" , "BEST" ]
print ( "The original list is : " + str (test_list))
res = defaultdict( int )
for ele in test_list:
res[ele.lower()] + = 1
print ( "Strings Frequency : " + str ( dict (res)))
|
Output
The original list is : ['Gfg', 'Best', 'best', 'gfg', 'GFG', 'is', 'IS', 'BEST']
Strings Frequency : {'gfg': 3, 'best': 3, 'is': 2}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2: Using count() method
Python3
test_list = [ "Gfg" , "Best" , "best" , "gfg" , "GFG" , "is" , "IS" , "BEST" ]
print ( "The original list is : " + str (test_list))
x = []
res = dict ()
for i in test_list:
x.append(i.lower())
a = list ( set (x))
for i in a:
res[i] = x.count(i)
print ( "Strings Frequency : " + str ( dict (res)))
|
Output
The original list is : ['Gfg', 'Best', 'best', 'gfg', 'GFG', 'is', 'IS', 'BEST']
Strings Frequency : {'is': 2, 'best': 3, 'gfg': 3}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: Using Counter() function
Python3
from collections import Counter
test_list = [ "Gfg" , "Best" , "best" , "gfg" , "GFG" , "is" , "IS" , "BEST" ]
print ( "The original list is : " + str (test_list))
for i in range ( len (test_list)):
test_list[i] = test_list[i].lower()
freq = Counter(test_list)
print ( "Strings Frequency : " + str ( dict (freq)))
|
Output
The original list is : ['Gfg', 'Best', 'best', 'gfg', 'GFG', 'is', 'IS', 'BEST']
Strings Frequency : {'gfg': 3, 'best': 3, 'is': 2}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: using operator.countOf() method
Python3
import operator as op
test_list = [ "Gfg" , "Best" , "best" , "gfg" , "GFG" , "is" , "IS" , "BEST" ]
print ( "The original list is : " + str (test_list))
x = []
res = dict ()
for i in test_list:
x.append(i.lower())
a = list ( set (x))
for i in a:
res[i] = op.countOf(x,i)
print ( "Strings Frequency : " + str ( dict (res)))
|
Output
The original list is : ['Gfg', 'Best', 'best', 'gfg', 'GFG', 'is', 'IS', 'BEST']
Strings Frequency : {'is': 2, 'gfg': 3, 'best': 3}
Time Complexity: O(N)
Auxiliary Space : O(N)
Method 5: Using a dictionary and for loop
Step-by-Step approach:
- Create an empty dictionary to store the frequency of strings.
- Loop through the strings in the list.
- Convert each string to lowercase using the str.lower() method.
- If the lowercase string is already in the dictionary, increase its value by 1.
- If the lowercase string is not in the dictionary, add it as a key with a value of 1.
- Print the dictionary.
Python3
test_list = [ "Gfg" , "Best" , "best" , "gfg" , "GFG" , "is" , "IS" , "BEST" ]
print ( "The original list is : " + str (test_list))
freq_dict = {}
for string in test_list:
lowercase_string = string.lower()
if lowercase_string in freq_dict:
freq_dict[lowercase_string] + = 1
else :
freq_dict[lowercase_string] = 1
print ( "Strings Frequency : " + str (freq_dict))
|
Output
The original list is : ['Gfg', 'Best', 'best', 'gfg', 'GFG', 'is', 'IS', 'BEST']
Strings Frequency : {'gfg': 3, 'best': 3, 'is': 2}
Time Complexity: O(n), where n is the number of strings in the list.
Auxiliary Space: O(n), where n is the number of strings in the list, to store the dictionary.
Similar Reads
Python | Case Counter in String
Sometimes, while working with Python String, we can have a problem in which we need to separate the lower and upper case count. This kind of operation can have its application in many domains. Let's discuss certain ways in which this task can be done. Method #1: Using map() + sum() + isupper() + isl
7 min read
Case-insensitive string comparison in Python
The goal here is to compare strings in a list without considering their case, meaning the comparison should ignore whether letters are uppercase or lowercase. For example, given the list ["hello", "HELLO", "HeLLo"], we want to check if all strings are equal, ignoring the case. Let's explore differen
2 min read
Case insensitive string replacement in Python
We are given a string and our task is to replace a specific word in it, ignoring the case of the letters. This means if we want to replace the word "best" with "good", then all variations like "BeSt", "BEST", or "Best" should also be replaced. For example, if the input is "gfg is BeSt", then the out
3 min read
Python | Convert string list into multiple cases
Sometimes, while working with Python strings, we might have a problem in which we have list of strings and we wish to convert them into specified cases. This problem generally occurs in the cases in which the strings we receive are ill-cased. Let's discuss certain ways in which this task can be perf
4 min read
Alternate cases in String - Python
The task of alternating the case of characters in a string in Python involves iterating through the string and conditionally modifying the case of each character based on its position. For example, given a string s = "geeksforgeeks", the goal is to change characters at even indices to uppercase and
3 min read
Python - Specific case change in String List
While working with String lists, the problem of cases is common, but sometimes, we are concerned about changing cases in strings selectively. i.e. on the basis of another list. This can have applications in day-day programming. Let us discuss certain ways in which this task can be performed. Method
7 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 - Convert case of elements in a list of strings
In Python, we often need to convert the case of elements in a list of strings for various reasons such as standardizing input or formatting text. Whether it's converting all characters to uppercase, lowercase, or even swapping cases. In this article, we'll explore several methods to convert the case
3 min read
Python - Lowercase Kth Character in string
The problem of lowercasing a string is quite common and has been discussed many times. But sometimes, we might have a problem like this in which we need to convert the Nth character of string to lowercase. Letâs discuss certain ways in which this can be performed. Method #1 : Using string slicing +
4 min read
Python - Check if string contains character
In Python, we can check if a string contains a character using several methods. in operator is the simplest and most commonly used way. If we need more control, methods like find(), index(), and count() can also be useful. Using in Operator in operator is the easiest way to check if a character exis
2 min read