Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Group strings at particular element in list

Improve Article
Save Article
  • Last Updated : 30 Jan, 2023
Improve Article
Save Article

Sometimes, while working with Python list, we can have a problem in which we have to group strings in a way that at occurrence of particular element, the string list is grouped. This can be a potential problem of day-day programming. Let’s discuss certain way in which this problem can be performed. 

Method : Using groupby() + list comprehension + lambda This task can be performed using combination of above functions. In this, we group the elements using groupby() and decision of grouping element logic is done in form of lambda function. 

Python3




# Python3 code to demonstrate working of
# Group strings at particular element in list
# using groupby() + list comprehension + lambda
from itertools import groupby
 
# initialize lists
test_list = ['gfg', 'is', 'best', 'and', 'is', 'popular']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize partition element
ele = 'is'
 
# Group strings at particular element in list
# using groupby() + list comprehension + lambda
res = [list(j) for i, j in groupby(test_list, lambda x:x == ele) if not i]
 
# printing result
print("Resultant list after grouping : " + str(res))

Output

The original list is : ['gfg', 'is', 'best', 'and', 'is', 'popular']
Resultant list after grouping : [['gfg'], ['best', 'and'], ['popular']]

Time complexity: O(n), where n is the length of the list “test_list”.
Auxiliary Space: O(m), where m is the number of resulting sublists in “res”.

Using re.split() function:

Using the re.split() function to split the list based on the grouping element is another approach. The time complexity would be O(n) as it needs to iterate through the list once, and the space complexity would be O(n) as it creates new sublists to store the grouped strings.

It’s important to note that using the re.split() function would require an import of the re module, and the implementation would involve a regular expression matching the desired grouping element to split the list by.

Python3




import re
 
# initialize list
test_list = ['gfg', 'is', 'best', 'and', 'is', 'popular']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize partition element
ele = 'is'
 
# Group strings at particular element in list
# using re.split()
res = [i.split() for i in re.split(ele, ' '.join(test_list))]
 
# printing result
print("Resultant list after grouping : " + str(res))
 
#This code is contributed by Edula Vinay Kumar Reddy

Output

The original list is : ['gfg', 'is', 'best', 'and', 'is', 'popular']
Resultant list after grouping : [['gfg'], ['best', 'and'], ['popular']]

This code first converts the list to a string, then splits the string by the grouping element ‘is’ using the re.split() function. The resulting list will be sublists with strings grouped based on the occurrence of ‘is’ in the original list

Method : Using split(),join() methods

Python3




# Python3 code to demonstrate working of
# Group strings at particular element in list
 
# initialize lists
test_list = ['gfg', 'is', 'best', 'and', 'is', 'popular']
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize partition element
ele = 'is'
 
# Group strings at particular element in list
def fun(y):
    p=[]
    for i in y:
        if len(i)!=0:
            p.append(i)
    return p
test_list="*".join(test_list)
x=test_list.split(ele)
res=[]
for i in x:
    y=i.split("*")
    res.append(fun(y))
# printing result
print("Resultant list after grouping : " + str(res))

Output

The original list is : ['gfg', 'is', 'best', 'and', 'is', 'popular']
Resultant list after grouping : [['gfg'], ['best', 'and'], ['popular']]

Time Complexity : O(N*N)
Auxiliary Space : O(N)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!