Python | Identical Strings Grouping
Last Updated :
09 Apr, 2023
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()
This particular function can prove to be quite useful to perform this particular task as it counts the frequency of Strings in the list and then we can pair them using the list comprehension.
Python3
# Python3 code to demonstrate
# Identical Strings Grouping
# using collections.Counter()
import collections
# initializing list
test_list = ["Gfg", "best", "is", "Gfg", "is", "best", "Gfg", "best"]
# printing original list
print("The original list : " + str(test_list))
# using collections.Counter()
# Identical Strings Grouping
temp = collections.Counter(test_list)
res = [[i] * j for i, j in temp.items()]
# print result
print("The Strings after grouping are : " + str(res))
Output : The original list : ['Gfg', 'best', 'is', 'Gfg', 'is', 'best', 'Gfg', 'best']
The Strings after grouping are : [['best', 'best', 'best'], ['Gfg', 'Gfg', 'Gfg'], ['is', 'is']]
The time complexity of the code is O(n), where n is the length of the input list.
The auxiliary space complexity of the code is also O(n), as the space required for the Counter object and the resulting list both depend on the number of unique strings in the input list, which can be at most n.
Method #2: Using itertools.groupby()
This problem can easily solved by the traditional groupby functionality that is offered by Python via groupby function, which groups the like elements as suggested by name.
Python3
# Python3 code to demonstrate
# Identical Strings Grouping
# using itertools.groupby()
import itertools
# initializing list
test_list = ["Gfg", "best", "is", "Gfg", "is", "best", "Gfg", "best"]
# printing original list
print("The original list : " + str(test_list))
# using itertools.groupby()
# Identical Strings Grouping
res = [list(i) for j, i in itertools.groupby(sorted(test_list))]
# print result
print("The Strings after grouping are : " + str(res))
Output : The original list : ['Gfg', 'best', 'is', 'Gfg', 'is', 'best', 'Gfg', 'best']
The Strings after grouping are : [['best', 'best', 'best'], ['Gfg', 'Gfg', 'Gfg'], ['is', 'is']]
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”.
Time complexity: The time complexity of this code is O(nlogn), where n is the length of the input list test_list.
Auxiliary space: The auxiliary space used by this code is O(n), where n is the length of the input list test_list.
Method #3 : Using count() method
Python3
# Python3 code to demonstrate
# Identical Strings Grouping
# initializing list
test_list = ["Gfg", "best", "is", "Gfg", "is", "best", "Gfg", "best"]
# printing original list
print("The original list : " + str(test_list))
res=[]
x=list(set(test_list))
x.sort()
for i in x:
a=[i]*test_list.count(i)
res.append(a)
# print result
print("The Strings after grouping are : " + str(res))
OutputThe original list : ['Gfg', 'best', 'is', 'Gfg', 'is', 'best', 'Gfg', 'best']
The Strings after grouping are : [['Gfg', 'Gfg', 'Gfg'], ['best', 'best', 'best'], ['is', 'is']]
Method #4 : Using operator.countOf() method
Python3
# Python3 code to demonstrate
# Identical Strings Grouping
# initializing list
test_list = ["Gfg", "best", "is", "Gfg", "is", "best", "Gfg", "best"]
# printing original list
print("The original list : " + str(test_list))
res=[]
x=list(set(test_list))
x.sort()
import operator
for i in x:
a=[i]*operator.countOf(test_list,i)
res.append(a)
# print result
print("The Strings after grouping are : " + str(res))
OutputThe original list : ['Gfg', 'best', 'is', 'Gfg', 'is', 'best', 'Gfg', 'best']
The Strings after grouping are : [['Gfg', 'Gfg', 'Gfg'], ['best', 'best', 'best'], ['is', 'is']]
Time Complexity : O(N)
Auxiliary Space : O(N)
METHOD 5: using a dictionary to group identical strings:
This method creates an empty dictionary res and iterates over the elements of the test_list. For each element s, it checks if it already exists in the dictionary. If it does, it appends s to the list corresponding to the key s. If it doesn't, it creates a new list with s as its only element and assigns it to the key s in the dictionary. Finally, it converts the dictionary values to a list and prints the result.
Python3
# Python3 code to demonstrate
# Identical Strings Grouping
# initializing list
test_list = ["Gfg", "best", "is", "Gfg", "is", "best", "Gfg", "best"]
# printing original list
print("The original list : " + str(test_list))
# using dictionary to group identical strings
res = {}
for s in test_list:
if s in res:
res[s].append(s)
else:
res[s] = [s]
# converting dictionary values to list
res = list(res.values())
# print result
print("The Strings after grouping are : " + str(res))
OutputThe original list : ['Gfg', 'best', 'is', 'Gfg', 'is', 'best', 'Gfg', 'best']
The Strings after grouping are : [['Gfg', 'Gfg', 'Gfg'], ['best', 'best', 'best'], ['is', 'is']]
The time complexity of the above Python code is O(n), where n is the length of the input list test_list
The auxiliary space complexity of the code is O(k), where k is the number of unique elements in the input list.
Similar Reads
Python - Case Insensitive Strings Grouping 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 c
4 min read
Python | Grouping similar substrings in list Sometimes we have an application in which we require to group common prefix strings into one such that further processing can be done according to the grouping. This type of grouping is useful in the cases of Machine Learning and Web Development. Let's discuss certain ways in which this can be done.
7 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 - Bigrams Frequency in String Sometimes while working with Python Data, we can have problem in which we need to extract bigrams from string. This has application in NLP domains. But sometimes, we need to compute the frequency of unique bigram for data collection. The solution to this problem can be useful. Lets discuss certain w
4 min read
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 Th
4 min read