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.
Share your thoughts in the comments
Please Login to comment...