Open In App

Python – Split Strings on Prefix Occurrence

Last Updated : 16 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a list of Strings, perform string split on the occurrence of prefix.

Input : test_list = [“geeksforgeeks”, “best”, “geeks”, “and”], pref = “geek” 
Output : [[‘geeksforgeeks’, ‘best’], [‘geeks’, ‘and’]] 
Explanation : At occurrence of string “geeks” split is performed.

Input : test_list = [“good”, “fruits”, “goodness”, “spreading”], pref = “good” 
Output : [[‘good’, ‘fruits’], [‘goodness’, ‘spreading’]] 
Explanation : At occurrence of string “good” split is performed. 

Method #1 : Using loop + startswith()

In this, we iterate each element of List, and check if new list has to be changed using startswith() by checking for prefix, and create new list if prefix is encountered.

Python3




# Python3 code to demonstrate working of
# Split Strings on Prefix Occurrence
# Using loop + startswith()
 
# initializing list
test_list = ["geeksforgeeks", "best", "geeks", "and", "geeks", "love", "CS"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing prefix
pref = "geek"
 
 
res = []
for val in test_list:
     
    # checking for prefix
    if val.startswith(pref):
         
        # if pref found, start new list
        res.append([val])
    else:
         
        # else append in current list
        res[-1].append(val)
 
# printing result
print("Prefix Split List : " + str(res))


Output

The original list is : ['geeksforgeeks', 'best', 'geeks', 'and', 'geeks', 'love', 'CS']
Prefix Split List : [['geeksforgeeks', 'best'], ['geeks', 'and'], ['geeks', 'love', 'CS']]

Method #2 : Using loop + zip_longest() + startswith()

In this, we zip all the elements with their subsequent element sublist and check for prefix using startswith(), if found, result is appended. 

Python3




# Python3 code to demonstrate working of
# Split Strings on Prefix Occurrence
# Using loop + zip_longest() + startswith()
from itertools import zip_longest
 
# initializing list
test_list = ["geeksforgeeks", "best", "geeks", "and", "geeks", "love", "CS"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing prefix
pref = "geek"
 
 
res, temp = [], []
 
for x, y in zip_longest(test_list, test_list[1:]):
    temp.append(x)
     
    # if prefix is found, split and start new list
    if y and y.startswith(pref):
        res.append(temp)
        temp = []
res.append(temp)
 
# printing result
print("Prefix Split List : " + str(res))


Output

The original list is : ['geeksforgeeks', 'best', 'geeks', 'and', 'geeks', 'love', 'CS']
Prefix Split List : [['geeksforgeeks', 'best'], ['geeks', 'and'], ['geeks', 'love', 'CS']]

Method #3 : Using list + recursion

Step-by-step approach:

  • Iterate over the input list of strings using a for loop:
    • For each string, check if it starts with the given prefix using the startswith() method.
    • If the string starts with the prefix, add the current sublist to the result list using the append() method, and create a new empty sublist by reassigning sublist to an empty list ([]).
    • If the string does not start with the prefix, add it to the current sublist using the append() method.
  • If the loop is complete, add the final sublist to the result list using the append() method.
  • Return the result list.
  • Test the function by calling it with a sample test_list and prefix, and print the output.

Python3




def split_list_at_prefix(test_list, pref):
    # Initialize an empty list to hold the sublists.
    result = []
    # Initialize an empty sublist to hold the strings that come before the prefix.
    sublist = []
    # Iterate over the input list of strings.
    for string in test_list:
        # For each string, check if it starts with the given prefix.
        if string.startswith(pref):
            # If the string starts with the prefix, add the current sublist to the result list,
            # and create a new empty sublist.
            if sublist:
                result.append(sublist)
                sublist = []
        # If the string does not start with the prefix, add it to the current sublist.
        sublist.append(string)
    # If the loop is complete, add the final sublist to the result list.
    if sublist:
        result.append(sublist)
    # Return the result list.
    return result
test_list = ['geeksforgeeks', 'best', 'geeks', 'and']
prefix = 'geek'
result = split_list_at_prefix(test_list, prefix)
print(result)


Output

[['geeksforgeeks', 'best'], ['geeks', 'and']]

Time complexity: O(n)
Auxiliary space: O(n)

Method #4 : Using loop + find() method

Step-by-step approach:

  • Initiate a for loop to traverse list elements
  • Check if new list has to be changed using find() by checking for prefix, and create new list if prefix is encountered.
  • Display new list

Python3




# Python3 code to demonstrate working of
# Split Strings on Prefix Occurrence
# Using loop + find()
 
# initializing list
test_list = ["geeksforgeeks", "best", "geeks", "and", "geeks", "love", "CS"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing prefix
pref = "geek"
res = []
for val in test_list:
    # checking for prefix
    if val.find(pref)==0:
        # if pref found, start new list
        res.append([val])
    else:
        # else append in current list
        res[-1].append(val)
# printing result
print("Prefix Split List : " + str(res))


Output

The original list is : ['geeksforgeeks', 'best', 'geeks', 'and', 'geeks', 'love', 'CS']
Prefix Split List : [['geeksforgeeks', 'best'], ['geeks', 'and'], ['geeks', 'love', 'CS']]

Time Complexity: O(N), where N length of list
Auxiliary Space: O(N)

Method #5: Using itertools.groupby() and zip():

Step-by-step approach:

  • Initialize the list and prefix to be used for splitting.
  • Use groupby function from itertools module on the original list with lambda function checking if each element starts with the prefix.
  • Loop through the groups produced by groupby. If a group starts with the prefix, append a new sublist containing only that element to the result list. Otherwise, append the elements to the last sublist in the result list.
  • Print the resulting list of sublists.

Python3




# Python3 code to demonstrate working of
# Split Strings on Prefix Occurrence
# Using itertools.groupby()
 
from itertools import groupby
 
# initializing list
test_list = ["geeksforgeeks", "best", "geeks", "and", "geeks", "love", "CS"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing prefix
pref = "geek"
 
# Using groupby() + startswith()
res = []
for k, g in groupby(test_list, lambda x: x.startswith(pref)):
    if k:
        res.append(list(g))
    else:
        if res:
            res[-1].extend(list(g))
 
# printing result
print("Prefix Split List : " + str(res))
#This code is contributed by Jyothi Pinjala.


Output

The original list is : ['geeksforgeeks', 'best', 'geeks', 'and', 'geeks', 'love', 'CS']
Prefix Split List : [['geeksforgeeks', 'best'], ['geeks', 'and'], ['geeks', 'love', 'CS']]

Time complexity: O(n), where n is the length of the input list. This is because the algorithm iterates over each element in the input list once.
Auxiliary space: O(n), where n is the length of the input list. This is because the algorithm creates a list of sublists, where each sublist contains some or all of the elements from the input list. The size of this list of sublists is proportional to the length of the input list.

Method #6: Using the reduce() function from the functools module

Python3




from functools import reduce
 
# initializing list of strings
test_list = ["geeksforgeeks", "best", "geeks", "and", "geeks", "love", "CS"]
 
# initializing prefix
pref = "geek"
 
# define function to split list of strings on prefix occurrence
def split_on_prefix(res, x):
    if x.startswith(pref):
        res.append([x])
    elif res:
        res[-1].append(x)
    return res
 
# apply function using reduce()
res = reduce(split_on_prefix, test_list, [])
 
# print result
print("Prefix Split List : " + str(res))


Output

Prefix Split List : [['geeksforgeeks', 'best'], ['geeks', 'and'], ['geeks', 'love', 'CS']]

Time complexity: O(n), where n is the length of the input list of strings, since it involves iterating through the list only once. 
Auxiliary space: O(m), where m is the number of prefix occurrences in the input list, since it involves creating a new list for each prefix occurrence.



Similar Reads

Python | Split strings in list with same prefix in all elements
Sometimes we face a problem in which we have a list of strings and there are some garbage/unwanted letters at its prefix or suffix or at the specified position uniformly, i.e this extends to all the strings in the list. Let's discuss certain ways in which this problem can be solved. Method #1 : Using list comprehension + list slicing This task can
5 min read
Python | Pandas Split strings into two List/Columns using str.split()
Pandas provide a method to split string around a passed separator/delimiter. After that, the string can be stored as a list in a series or it can also be used to create multiple column data frames from a single separated string. It works similarly to Python's default split() method but it can only be applied to an individual string. Pandas <code
4 min read
Python | Split on last occurrence of delimiter
The splitting of strings has always been discussed in various applications and use cases. One of the interesting variations of list splitting can be splitting the list on delimiter but this time only on the last occurrence of it. Let's discuss certain ways in which this can be done. Method #1: Using rsplit(str, 1) The normal string split can perfor
7 min read
Python | Split string on Kth Occurrence of Character
Many problems of the split have been dealt with in the past, but sometimes, one can also encounter this problem in which we need to split the string on the Kth occurrence of a character. Let's discuss certain ways in which this problem can be solved. Method #1 : Using split() + join() The split and join method can perform this particular task. In t
6 min read
Prefix Factorials of a Prefix Sum Array
Given an array arr[] consisting of N positive integers, the task is to find the prefix factorials of a prefix sum array of the given array i.e., [Tex]prefix[i] = (\sum_{0}^{i}arr[i])! [/Tex]. Examples: Input: arr[] = {1, 2, 3, 4}Output: 1 6 720 3628800Explanation:The prefix sum of the given array is {1, 3, 6, 10}. Therefore, prefix factorials of th
10 min read
Generate two output strings depending upon occurrence of character in input string in Python
Given an input string str[], generate two output strings. One of which consists of that character that occurs only once in the input string and the second consists of multi-time occurring characters. Output strings must be sorted. Examples: Input : str = "geeksforgeeks" Output : String with characters occurring once: "for". String with characters o
4 min read
Python Program to print strings with repetitive occurrence of an element in a list
Given a strings List, write a Python program that extracts all the strings with more than one occurrence of a specific value(here described using K) in elements of a list. Examples: Input : test_list = ["geeksforgeeks", "best", "for", "geeks"], K = 'e' Output : ['geeksforgeeks', 'geeks'] Explanation : geeks and geeksforgeeks have 2 and 4 occurrence
5 min read
Python | Ways to determine common prefix in set of strings
Given a set of strings, write a Python program to determine common prefix from a set of strings. Given below are a few methods to solve the above task.Method #1: Using Naive Approach C/C++ Code # Python code to demonstrate # to find common prefix # from set of strings # Initialising string ini_strlist = ['akshat', 'akash', 'akshay', 'akshita'] # Fi
2 min read
Python | Merging two strings with Suffix and Prefix
Given two strings A and B and these strings contain lower case letters. The task is to tell the length of the merged strings. For example, given A is "abcde" and B is "cdefg", then merging the two strings results in "abcdefg". The merge operation is performed in such a manner that the joining characters are both the suffix of A and Prefix of B. Bef
2 min read
Python | Append suffix/prefix to strings in list
Sometimes, while working with Python, we can a problem in which we need to pad strings in lists at trailing or leading position. This kind of problem is quite common and can occur in day-day programming or web development. Let's discuss a way in which this task can be performed. Method #1: Using + operator + list comprehension In this task, we just
5 min read
Practice Tags :
three90RightbarBannerImg