Python - Groups Strings on Kth character
Last Updated :
28 Apr, 2023
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
# 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)))
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
# 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))
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
# Python3 code to demonstrate working of
# Groups Strings on Kth character
# Using list comprehension
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 list comprehension
res = defaultdict(list)
[res[word[K-1]].append(word) for word in test_list]
# printing result
print("The strings grouping : " + str(dict(res)))
OutputThe 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
# 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 itertools.groupby()
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)
# printing result
print("The strings grouping : " + str(dict(res)))
OutputThe 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.
Similar Reads
Python | Group List on K character Sometimes, we may face an issue in which we require to split a list to list of list on the K character sent as deliminator. This kind of problem can be used to send messages or can be used in cases where it is desired to have list of list of native list. Letâs discuss certain ways in which this can
3 min read
Python - Group list by first character of string 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 types of problems are typical to database queries and hence can occur in web development while programming. This article focuses on one such grouping by
7 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
Get Last N characters of a string - Python We are given a string and our task is to extract the last N characters from it. For example, if we have a string s = "geeks" and n = 2, then the output will be "ks". Let's explore the most efficient methods to achieve this in Python.Using String Slicing String slicing is the fastest and most straigh
2 min read
Python - Test if Kth character is digit in String Given a String, check if Kth index is a digit. Input : test_str = 'geeks9geeks', K = 5 Output : True Explanation : 5th idx element is 9, a digit, hence True.Input : test_str = 'geeks9geeks', K = 4 Output : False Explanation : 4th idx element is s, not a digit, hence False. Method #1: Using in operat
5 min read
Split String of list on K character in Python In this article, we will explore various methods to split string of list on K character in Python. The simplest way to do is by using a loop and split().Using Loop and split()In this method, we'll iterate through each word in the list using for loop and split it based on given K character using spli
2 min read
Python | K Character Split String The problems and at the same time applications of list splitting is quite common while working with python strings. Some characters are usually tend to ignore in the use cases. But sometimes, we might not need to omit those characters but include them in our programming output. Letâs discuss certain
4 min read
Python | Kth index character similar Strings Sometimes, we require to get the words that have the Kth index with the specific letter. This kind of use case is quiet common in places of common programming projects or competitive programming. Letâs discuss certain shorthand to deal with this problem in Python. Method #1: Using list comprehension
3 min read
Python | Longest Run of given Character in String Sometimes, while working with Strings, we can have a problem in which we need to perform the extraction of length of longest consecution of certain letter. This can have application in web development and competitive programming. Lets discuss certain ways in which this task can be performed. Method
6 min read
Python | Split string in groups of n consecutive characters Given a string (be it either string of numbers or characters), write a Python program to split the string by every nth character. Examples: Input : str = "Geeksforgeeks", n = 3 Output : ['Gee', 'ksf', 'org', 'eek', 's'] Input : str = "1234567891234567", n = 4 Output : [1234, 5678, 9123, 4567] Method
2 min read