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
import collections
test_list = [ "Gfg" , "best" , "is" , "Gfg" , "is" , "best" , "Gfg" , "best" ]
print ( "The original list : " + str (test_list))
temp = collections.Counter(test_list)
res = [[i] * j for i, j in temp.items()]
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
import itertools
test_list = [ "Gfg" , "best" , "is" , "Gfg" , "is" , "best" , "Gfg" , "best" ]
print ( "The original list : " + str (test_list))
res = [ list (i) for j, i in itertools.groupby( sorted (test_list))]
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
test_list = [ "Gfg" , "best" , "is" , "Gfg" , "is" , "best" , "Gfg" , "best" ]
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 ( "The Strings after grouping are : " + str (res))
|
Output
The 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
test_list = [ "Gfg" , "best" , "is" , "Gfg" , "is" , "best" , "Gfg" , "best" ]
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 ( "The Strings after grouping are : " + str (res))
|
Output
The 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
test_list = [ "Gfg" , "best" , "is" , "Gfg" , "is" , "best" , "Gfg" , "best" ]
print ( "The original list : " + str (test_list))
res = {}
for s in test_list:
if s in res:
res[s].append(s)
else :
res[s] = [s]
res = list (res.values())
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 : [['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
Lists 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. [GFGTABS
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 Lis
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 - Disjoint Strings across Lists
Given two lists, extract all the string pairs which are disjoint across i.e. which don't have any character in common. Input : test_list1 = ["haritha", "is", "best"], test_list2 = ["she", "loves", "papaya"] Output : [('haritha', 'loves'), ('is', 'papaya'), ('best', 'papaya')] Explanation : "is" and
4 min read
Python - Formable Strings Count in Matrix
Given strings matrix, the task is to write a Python program to count strings that can be made from letters from the given list. Examples: Input : test_list = [["gfg", "best"], ["all", "love", "gfg"], ["gfg", "is", "good"], ["geeksforgeeks"]], tar_list = ["g", "f", "s", "b", "o", "d", "e", "t"] Outpu
5 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