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
String Interning in Python
String interning is a memory optimization technique used in Python to enhance the efficiency of string handling. In Python, strings are immutable, meaning their values cannot be changed after creation. String interning, or interning strings, involves reusing existing string objects rather than creat
2 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
List of strings in Python
A list of strings in Python stores multiple strings together. In this article, weâll explore how to create, modify and work with lists of strings using simple examples.Creating a List of StringsWe can use square brackets [] and separate each string with a comma to create a list of strings.Pythona =
2 min read
Python - Words Lengths in String
We are given a string we need to find length of each word in a given string. For example, we are s = "Hello world this is Python" we need to find length of each word so that output should be a list containing length of each words in sentence, so output in this case will be [5, 5, 4, 2, 6].Using List
2 min read
Find Length of String in Python
In this article, we will learn how to find length of a string. Using the built-in function len() is the most efficient method. It returns the number of items in a container. Pythona = "geeks" print(len(a)) Output5 Using for loop and 'in' operatorA string can be iterated over, directly in a for loop.
2 min read
Put String in A Set in Python
Imagine you have a magical box in Python that only stores unique items â that's exactly what a set is! Sets are like special containers designed to hold a bunch of different things without any duplicates. In Python, they provide a versatile and efficient way to manage collections of distinct element
3 min read
Python - Combine Strings to Matrix
Sometimes while working with data, we can receive separate data in the form of strings and we need to compile them into Matrix for its further use. This can have applications in many domains. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + split
4 min read
Python | Consecutive String Comparison
Sometimes, while working with data, we can have a problem in which we need to perform comparison between a string and it's next element in a list and return all strings whose next element is similar list. Let's discuss certain ways in which this task can be performed. Method #1 : Using zip() + loop
3 min read