Open In App

Python | Group strings at particular element in list

Last Updated : 06 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 1: 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”.

Method 2: 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

Time complexity: The time complexity of this code is O(n), where n is the length of the input list test_list.
Auxiliary space: The auxiliary space complexity of this code is O(n), where n is the length of the input list test list

Method 3: 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)

Method 4: Using a loop and the enumerate function 

The idea is to use a loop and the enumerate function to iterate through the original list and split it into sublists based on the partition element. It uses a start index variable to keep track of the beginning of each sublist, and appends each sublist to a result list as it is generated.

Implementation:

Python3




test_list = ['gfg', 'is', 'best', 'and', 'is', 'popular']
ele = 'is'
 
# Group strings at particular element in list
res = []
start = 0
for i, item in enumerate(test_list):
    if item == ele:
        res.append(test_list[start:i])
        start = i+1
res.append(test_list[start:])
 
# printing original list
print("The original list is : " + str(test_list))
 
# 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 input list.
Auxiliary Space: O(N)

Method 5: Using itertools.groupby()

Algorithm:

  1. Import groupby from itertools module.
  2. Initialize an empty list res.
  3. Use groupby method to group the elements of the list based on the element ele.
  4. Check if the key is not equal to the element ele.
  5. If the key is not equal to ele, then append the corresponding group to the result list.
  6. Return the result list.

Python3




from itertools import groupby
 
test_list = ['gfg', 'is', 'best', 'and', 'is', 'popular']
ele = 'is'
 
res = [list(group) for key, group in groupby(test_list, lambda x: x == ele) if not key]
 
# printing original list
print("The original list is : " + str(test_list))
 
# 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 input list.
Auxiliary Space: O(n), where n is the length of the input list.

Method 6: using numpy:

Algorithmic :

1.Convert the input list into a numpy array. Time complexity: O(N), where N is the length of the input list.
2.Use np.where to get the indices of the occurrences of the given element in the array. Time complexity: O(N), where N is the length of the input list.
3.Loop through the indices and split the array into sub-arrays based on those indices. Time complexity: O(M), where M is the number of 4.occurrences of the given element in the input list.
5.Append each sub-array to the output list. 

Python3




import numpy as np
 
test_list = ['gfg', 'is', 'best', 'and', 'is', 'popular']
ele = 'is'
 
split_indices = np.where(np.array(test_list) == ele)[0]
res = []
start = 0
for index in split_indices:
    res.append(test_list[start:index])
    start = index + 1
res.append(test_list[start:])
 
print("The original list is : " + str(test_list))
print("Resultant list after grouping : " + str(res))
#This code is contributed by Jyothi pinjala


Output:

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

Time Complexity: The time complexity of this algorithm is O(N + M), where N is the length of the input list and M is the number of occurrences of the given element in the input list.

Space Complexity: The space complexity of this algorithm is O(M), where M is the number of occurrences of the given element in the input list. This is because we need to store the indices of the occurrences in memory in order to split the list into sub-lists. Additionally, we need to store the output list, which also takes up O(M) space in the worst case.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads