
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Split Strings on Prefix Occurrence
When it is required to split the strings based on the occurrence of the prefix, two empty lists are defined, and a prefix value is defined. A simple iteration is used along with ‘append’ method.
Example
Below is a demonstration of the same −
from itertools import zip_longest my_list = ["hi", 'hello', 'there',"python", "object", "oriented", "object", "cool", "language", 'py','extension', 'bjarne'] print("The list is : " ) print(my_list) my_prefix = "python" print("The prefix is :") print(my_prefix) my_result, my_temp_val = [], [] for x, y in zip_longest(my_list, my_list[1:]): my_temp_val.append(x) if y and y.startswith(my_prefix): my_result.append(my_temp_val) my_temp_val = [] my_result.append(my_temp_val) print("The resultant is : " ) print(my_result) print("The list after sorting is : ") my_result.sort() print(my_result)
Output
The list is : ['hi', 'hello', 'there', 'python', 'object', 'oriented', 'object', 'cool', 'language', 'py', 'extension', 'bjarne'] The prefix is : python The resultant is : [['hi', 'hello', 'there'], ['python', 'object', 'oriented', 'object', 'cool', 'language', 'py', 'extension', 'bjarne']] The list after sorting is : [['hi', 'hello', 'there'], ['python', 'object', 'oriented', 'object', 'cool', 'language', 'py', 'extension', 'bjarne']]
Explanation
The required packages are imported into the environment.
A list of strings is defined and is displayed on the console.
The prefix value is defined and is displayed on the console.
Two empty lists are defined.
The ‘zip_longest’ method is used to combine the list along with same list by omitting the first value in an iteration.
The elements are appended to one of the empty list.
This list is displayed as the output on the console.
This list is again sorted and displayed on the console.
- Related Articles
- How to split strings on multiple delimiters with Python?
- Python Program to print strings based on the list of prefix
- Program to perform prefix compression from two strings in Python
- Maximum occurrence of prefix in the Array in C++
- Program to find longest common prefix from list of strings in Python
- Program to split two strings to make palindrome using Python
- Split Concatenated Strings in C++
- Generate two output strings depending upon occurrence of character in input string in Python
- Python – Split List on next larger value
- Numbers in Java (With 0 Prefix and with Strings)
- Find the longest common prefix between two strings after performing swaps on second string in C++
- Sort tuple based on occurrence of first element in Python
- Check if given string can be split into four distinct strings in Python
- How to split string on whitespace in Python?
- K Prefix in Python

Advertisements