Python - Case Insensitive Strings Grouping
Last Updated :
16 May, 2023
Sometimes, we have a use case in which we need to perform the grouping of strings by various factors, like first letter or any other factor. These type of problems are typical to database queries and hence can occur in web development while programming. This article focuses on one such grouping by case insensitivity i.e grouping all strings which are same but have different cases. Let’s discuss certain ways in which this can be performed.
Method #1 : Using next() + lambda + loop The combination of above 3 functions is used to solve this particular problem by the naive method. The lambda function performs the task of finding like cases, and next function helps in forward iteration.
Python3
# Python3 code to demonstrate
# Case Insensitive Strings Grouping
# using next() + lambda + loop
# initializing list
test_list = ['man', 'a', 'gEek', 'for', 'GEEK', 'FoR']
# printing original list
print("The original list : " + str(test_list))
# using next() + lambda + loop
# Case Insensitive Strings Grouping
util_func = lambda x, y: str.lower(x) == str.lower(y)
res = []
for sub in test_list:
ele = next((x for x in res if util_func(sub, x[0])), [])
if ele == []:
res.append(ele)
ele.append(sub)
# print result
print("The list after Categorization : " + str(res))
Output : The original list : ['man', 'a', 'gEek', 'for', 'GEEK', 'FoR']
The list after Categorization : [['man'], ['a'], ['gEek', 'GEEK'], ['for', 'FoR']]
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #2 : Using sorted() + groupby() This particular task can also be solved using the groupby function which offers a conventional method to solve this problem. The sorted function sorts the elements by size to be feed to groupby for the relevant grouping.
Python3
# Python3 code to demonstrate
# Case Insensitive Strings Grouping
# using sorted() + groupby()
from itertools import groupby
# initializing list
test_list = ['man', 'a', 'gEek', 'for', 'GEEK', 'FoR']
# printing original list
print("The original list : " + str(test_list))
# using sorted() + groupby()
# Case Insensitive Strings Grouping
util_func = lambda x: str.lower(x)
temp = sorted(test_list, key = util_func)
res = [list(ele) for i, ele in groupby(temp, util_func)]
# print result
print("The list after Categorization : " + str(res))
Output : The original list : ['man', 'a', 'gEek', 'for', 'GEEK', 'FoR']
The list after Categorization : [['man'], ['a'], ['gEek', 'GEEK'], ['for', 'FoR']]
Time Complexity: O(n*logn), where n is length of test_list.
Auxiliary Space: O(m), where m is length of res list.
Method #3 : Using Dictionary
Python3
# Python3 code to demonstrate
# Case Insensitive Strings Grouping
res = {}
# initializing list
test_list = ['man', 'a', 'gEek', 'for', 'GEEK', 'FoR']
# printing original list
print("The original list : " + str(test_list))
for i in test_list:
if i.lower() in res.keys():
res[i.lower()].append(i)
else:
res[i.lower()] = [i]
# print result
print("The list after Categorization : " + str(list(res.values())))
OutputThe original list : ['man', 'a', 'gEek', 'for', 'GEEK', 'FoR']
The list after Categorization : [['man'], ['a'], ['gEek', 'GEEK'], ['for', 'FoR']]
Method #4 : Using reduce
In this method we iterates over each element of the test_list using reduce. The lambda function checks whether the lowercase version of the current element exists in the keys. If it does, the current element is appended to the value of the corresponding key. If it doesn't, a new key is created in the list using the lowercase version of the current element and its value is set to a list containing the current element. Finally, the list of values in the list is returned using the list() function.
Python3
# Python3 code to demonstrate
# Case Insensitive Strings Grouping
from functools import reduce
# initializing list
test_list = ['man', 'a', 'gEek', 'for', 'GEEK', 'FoR']
# printing original list
print("The original list : " + str(test_list))
# Case Insensitive Strings Grouping using reduce() + lambda function
res = reduce(lambda x, y: (x.update({y.lower(): x.get(y.lower(), []) + [y]})) or x, test_list, {})
# print result
print("The list after Categorization : " + str(list(res.values())))
OutputThe original list : ['man', 'a', 'gEek', 'for', 'GEEK', 'FoR']
The list after Categorization : [['man'], ['a'], ['gEek', 'GEEK'], ['for', 'FoR']]
Time Complexity: O(n), where n is the number of elements in the test_list
Auxiliary Space: O(n), because it creates a dictionary to store the categorized elements, and the size of the dictionary grows with the number of elements in the input list.
Similar Reads
Python - Case Insensitive string counter 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 occu
4 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
Python | Identical Strings Grouping Sometimes, we need to perform the conventional task of grouping some like Strings into a separate list and thus forming a list of list. This can also help in counting and also get the sorted order of elements. Letâs discuss certain ways in which this can be done. Method #1: Using collections.Counter
5 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 - Group contiguous strings in List Given a mixed list, the task is to write a Python program to group all the contiguous strings. Input : test_list = [5, 6, 'g', 'f', 'g', 6, 5, 'i', 's', 8, 'be', 'st', 9] Output : [5, 6, ['g', 'f', 'g'], 6, 5, ['i', 's'], 8, ['be', 'st'], 9] Explanation : Strings are grouped to form result.Input : t
5 min read
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