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 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 brute force approach. We iterate each string, and group the dictionary after conditional check using conditional statement.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Groups Strings on Kth character
# Using loop
from collections import defaultdict
  
# initializing list
test_list = ["gfg", "is", "best", "for", "geeks"]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K 
K = 2
  
# Groups Strings on Kth character
# Using loop
res = defaultdict(list)
for word in test_list:
    res[word[K - 1]].append(word)
  
# printing result 
print("The strings grouping : " + str(dict(res))) 

chevron_right


Output :

The original list is : [‘gfg’, ‘is’, ‘best’, ‘for’, ‘geeks’]
The strings grouping : {‘f’: [‘gfg’], ‘s’: [‘is’], ‘e’: [‘best’, ‘geeks’], ‘o’: [‘for’]}

 



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().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Groups Strings on Kth character
# Using loop + map()
  
# initializing list
test_list = ["gfg", "is", "best", "for", "geeks"]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K 
K = 2
  
# Groups Strings on Kth character
# Using loop + map()
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
  
# printing result 
print("The strings grouping : " + str(res)) 

chevron_right


Output :

The original list is : [‘gfg’, ‘is’, ‘best’, ‘for’, ‘geeks’]
The strings grouping : {‘f’: [‘gfg’], ‘s’: [‘is’], ‘e’: [‘best’, ‘geeks’], ‘o’: [‘for’]}




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.