Python - Rear character String categorization
Last Updated :
12 Apr, 2023
Sometimes, we have a use case in which we need to perform the grouping of strings by various factors, like last 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 last letter of string. 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 rear character, and next function helps in forward iteration.
Python3
# Python3 code to demonstrate
# Rear character String categorization
# using next() + lambda + loop
# initializing list
test_list = ['an', 'apple', 'geek', 'for', 'greek', 'free']
# printing original list
print("The original list : " + str(test_list))
# using next() + lambda + loop
# Rear character String categorization
util_func = lambda x, y: x[len(x) - 1] == y[len(y) - 1]
res = []
for sub in test_list:
ele = next((x for x in res if util_func(sub, x[len(x) - 1])), [])
if ele == []:
res.append(ele)
ele.append(sub)
# print result
print("The list after Categorization : " + str(res))
Output : The original list : ['an', 'apple', 'geek', 'for', 'greek', 'free']
The list after Categorization : [['an'], ['apple', 'free'], ['geek', 'greek'], ['for']]
Time Complexity: O(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 rear character to be feed to groupby for the relevant grouping.
Python3
# Python3 code to demonstrate
# Rear character String categorization
# using sorted() + groupby()
from itertools import groupby
# initializing list
test_list = ['an', 'apple', 'geek', 'for', 'greek', 'free']
# printing original list
print("The original list : " + str(test_list))
# using sorted() + groupby()
# Rear character String categorization
util_func = lambda x: x[len(x) - 1]
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 : ['an', 'apple', 'geek', 'for', 'greek', 'free']
The list after Categorization : [['an'], ['apple', 'free'], ['geek', 'greek'], ['for']]
Time Complexity: O(n*nlogn), 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 #3: Using endswith() and for loops
In this method, we will create a new list with all the unique ending characters of the words from the given list, then we can group all the words based on their last characters using endswith() function.
Python3
# Python3 code to demonstrate
# Rear character String categorization
# initializing list
test_list = ['an', 'apple', 'geek', 'for', 'greek', 'free']
# printing original list
print("The original list : " + str(test_list))
x = []
for i in test_list:
if i[-1] not in x:
x.append(i[-1])
res = []
for i in x:
x = []
for j in test_list:
if(j.endswith(i)):
x.append(j)
res.append(x)
# print result
print("The list after Categorization : " + str(res))
OutputThe original list : ['an', 'apple', 'geek', 'for', 'greek', 'free']
The list after Categorization : [['an'], ['apple', 'free'], ['geek', 'greek'], ['for']]
Method #4: Using defaultdict
- Import defaultdict from the collections module.
- Initialize the test_list.
- Create an empty defaultdict with a list as its default value. This means that if we try to access a key that doesn't exist in the dictionary, it will automatically create a new empty list for that key.
- Iterate through the test_list, and for each string, append it to the list corresponding to its last character in the defaultdict.
- Finally, print out the values of the dictionary as a list.
Python3
# Python3 code to demonstrate
# Rear character String categorization
# using defaultdict
from collections import defaultdict
# initializing list
test_list = ['an', 'apple', 'geek', 'for', 'greek', 'free']
# printing original list
print("The original list : " + str(test_list))
# using defaultdict
# Rear character String categorization
d = defaultdict(list)
for s in test_list:
d[s[-1]].append(s)
# print result
print("The list after Categorization : " + str(list(d.values())))
OutputThe original list : ['an', 'apple', 'geek', 'for', 'greek', 'free']
The list after Categorization : [['an'], ['apple', 'free'], ['geek', 'greek'], ['for']]
Time complexity: O(n)
Auxiliary space: O(n)
Similar Reads
Reverse Alternate Characters in a String - Python
Reversing alternate characters in a string involves rearranging the characters so that every second character is reversed while maintaining the original order of other characters. For example, given the string 'abcde', reversing the alternate characters results in 'ebcda', where the first, third and
3 min read
Python - Capitalize repeated characters in a string
Given an input string with lowercase letters, the task is to write a python program to identify the repeated characters in the string and capitalize them. Examples: Input: programming languageOutput: pRoGRAMMiNG lANGuAGeExplanation: r,m,n,a,g are repeated elements Input: geeks for geeksOutput: GEEKS
4 min read
Python | First character occurrence from rear String
There are many ways to find out the first index of element in String as python in its language provides index() function that returns the index of first occurrence of element in String. But if one desires to get the last occurrence of element in string, usually a longer method has to be applied. Let
4 min read
Python - Phrase removal in String
Sometimes, while working with Python strings, we can have a problem in which we need to extract certain words in a string excluding the initial and rear K words. This can have application in many domains including all those include data. Lets discuss certain ways in which this task can be performed.
2 min read
Python | Rear stray character String split
Python3 # Python3 code to demonstrate working of # Rear stray character String split # Using list comprehension # initializing string test_str = 'gfg, is, best, ' # printing original string print("The original string is : " + test_str) # Rear stray character String split # Using list compr
5 min read
Python - Sort by Rear Character in Strings List
Given a String list, perform sort by the rear character in the Strings list. Input : test_list = ['gfg', 'is', 'for', 'geeks'] Output : ['gfg', 'for', 'is', 'geeks'] Explanation : g < r < s = s, hence the order. Input : test_list = ['gfz', 'is', 'for', 'geeks'] Output : ['for', 'is', 'geeks',
5 min read
Python | Strings with similar front and rear character
Sometimes, while programming, we can have a problem in which we need to check for the front and rear characters of each string. We may require to extract the count of all strings with similar front and rear characters. Let's discuss certain ways in which this task can be performed. Method #1: Using
4 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 - Extract range characters from String
Given a String, extract characters only which lie between given letters. Input : test_str = 'geekforgeeks is best', strt, end = "g", "s" Output : gkorgksiss Explanation : All characters after g and before s are retained. Input : test_str = 'geekforgeeks is best', strt, end = "g", "r" Output : gkorgk
4 min read
Python | Categorize the given list by string size
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 s
6 min read