Python – Groups Strings on Kth character
Sometimes, while working with Python Strings, we can have a problem in which we need to perform Grouping of Python Strings on the basis of its Kth character. This kind of problem can come in day-day programming. Let’s discuss certain ways in which this task can be performed.
Method #1: Using loop
This is one way in which this task can be performed. In this, we perform the task of grouping using a brute force approach. We iterate each string, and group the dictionary after a conditional check using a conditional statement.
Python3
from collections import defaultdict
test_list = [ "gfg" , "is" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
K = 2
res = defaultdict( list )
for word in test_list:
res[word[K - 1 ]].append(word)
print ( "The strings grouping : " + str ( dict (res)))
|
Output :
The original list is : [‘gfg’, ‘is’, ‘best’, ‘for’, ‘geeks’] The strings grouping : {‘f’: [‘gfg’], ‘s’: [‘is’], ‘e’: [‘best’, ‘geeks’], ‘o’: [‘for’]}
Time complexity: O(n), where n is the number of elements in the test_list.
Auxiliary space: O(n), where n is the number of elements in the test_list.
Method #2: Using map() + loop
This is yet another way to solve this problem. In this variant, additional test of valid character is added using map().
Python3
test_list = [ "gfg" , "is" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
K = 2
res = dict ()
for char in map ( chr , range ( 97 , 123 )):
words = [idx for idx in test_list if idx[K - 1 ] = = char]
if words:
res[char] = words
print ( "The strings grouping : " + str (res))
|
Output :
The original list is : [‘gfg’, ‘is’, ‘best’, ‘for’, ‘geeks’] The strings grouping : {‘f’: [‘gfg’], ‘s’: [‘is’], ‘e’: [‘best’, ‘geeks’], ‘o’: [‘for’]}
Time Complexity: O(N*N) where n is the number of elements in the dictionary. map() + loop is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(N) additional space of size n is created where n is the number of elements in the dictionary.
Method #3: Using list comprehension
- Initializing list
- Printing original list
- Initializing K
- Groups Strings on Kth character Using list comprehension
- Printing result
Python3
from collections import defaultdict
test_list = [ "gfg" , "is" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
K = 2
res = defaultdict( list )
[res[word[K - 1 ]].append(word) for word in test_list]
print ( "The strings grouping : " + str ( dict (res)))
|
Output
The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
The strings grouping : {'f': ['gfg'], 's': ['is'], 'e': ['best', 'geeks'], 'o': ['for']}
Time complexity: O(n), where n is the number of words in the input list.
Auxiliary space: O(kn), where k is the number of unique Kth characters and n is the number of words in the input list.
Method 4: Using itertools.groupby() method
Python3
from itertools import groupby
from operator import itemgetter
from collections import defaultdict
test_list = [ "gfg" , "is" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
K = 2
test_list.sort(key = lambda x: x[K - 1 ])
res = defaultdict( list )
for k, g in groupby(test_list, key = itemgetter(K - 1 )):
res[k].extend(g)
print ( "The strings grouping : " + str ( dict (res)))
|
Output
The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
The strings grouping : {'e': ['best', 'geeks'], 'f': ['gfg'], 'o': ['for'], 's': ['is']}
Time complexity: O(NlogN), where N is the length of the input list.
Auxiliary space: O(N), due to the creation of a defaultdict and a sorted copy of the input list.
Last Updated :
28 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...