Split String of list on K character in Python (original) (raw)
Last Updated : 05 Dec, 2024
In this article, we will explore various methods to split string of list on K character in Python. The simplest way to do is by using a **loop and split().
Using Loop and **split()
In this method, we'll iterate through each word in the list using for loop and split it based on given K character using s**plit() method.
Python `
a = ['Gfg is best', 'for Geeks', 'Preparing']
Character to split on (space)
k = ' '
Initialize an empty list to store the result
res = []
Loop through each string in the list
for word in a:
# Split the string at each space 'K'
split_word = word.split(k)
res.append(split_word)
print(res)
`
Output
[['Gfg', 'is', 'best'], ['for', 'Geeks'], ['Preparing']]
**Explanation:
- **word.split(K): split() method splits each word at every occurrence of **K.
- For word '**Gfg is best', it splits into ['Gfg', 'is', 'best'].
- For the word '**for Geeks', it splits into **['for', 'Geeks'].
Using List Comprehension
List comprehension is a more concise and Pythonic way to perform the above method.
Python `
a = ['Gfg is best', 'for Geeks', 'Preparing']
Character to split on (space)
K = ' '
Using list comprehension to split
each string in the list on the space character
res = [word.split(K) for word in a]
print(res)
`
Output
[['Gfg', 'is', 'best'], ['for', 'Geeks'], ['Preparing']]
**Explanation:
- **List comprehension iterates over each word in the list a and splits it at every occurrence of **K
- **split(K) performs the split operation and resulting substrings are collected into a list.
Similar Reads
- Split String into List of characters in Python We are given a string and our task is to split this string into a list of its individual characters, this can happen when we want to analyze or manipulate each character separately. For example, if we have a string like this: 'gfg' then the output will be ['g', 'f', 'g'].Using ListThe simplest way t 2 min read
- Splitting String to List of Characters - Python The task of splitting a string into a list of characters in Python involves breaking down a string into its individual components, where each character becomes an element in a list. For example, given the string s = "GeeksforGeeks", the task is to split the string, resulting in a list like this: ['G 3 min read
- Splitting String to List of Characters - Python We are given a string, and our task is to split it into a list where each element is an individual character. For example, if the input string is "hello", the output should be ['h', 'e', 'l', 'l', 'o']. Let's discuss various ways to do this in Python.Using list()The simplest way to split a string in 2 min read
- Python | K Character Split String The problems and at the same time applications of list splitting is quite common while working with python strings. Some characters are usually tend to ignore in the use cases. But sometimes, we might not need to omit those characters but include them in our programming output. Let’s discuss certain 4 min read
- Split string on Kth Occurrence of Character - Python The task is to write Python program to split a given string into two parts at the Kᵗʰ occurrence of a specified character. If the character occurs fewer than K times return the entire string as the first part and an empty string as the second part. For example, in the string "a,b,c,d,e,f", splitting 3 min read
- Python | Remove last character in list of strings Sometimes, we come across an issue in which we require to delete the last character from each string, that we might have added by mistake and we need to extend this to the whole list. This type of utility is common in web development. Having shorthands to perform this particular job is always a plus 8 min read
- Iterate over characters of a string in Python In this article, we will learn how to iterate over the characters of a string in Python. There are several methods to do this, but we will focus on the most efficient one. The simplest way is to use a loop. Let’s explore this approach.Using for loopThe simplest way to iterate over the characters in 2 min read
- Python | Split flatten String List Sometimes, while working with Python Strings, we can have problem in which we need to perform the split of strings on a particular deliminator. In this, we might need to flatten this to a single String List. Let's discuss certain ways in which this task can be performed. Method #1 : Using list compr 7 min read
- Python - Group list by first character of string 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 types of problems are typical to database queries and hence can occur in web development while programming. This article focuses on one such grouping by 7 min read
- Python - Sort String list by K character frequency Given String list, perform sort operation on basis of frequency of particular character. Input : test_list = ["geekforgeekss", "is", "bessst", "for", "geeks"], K = 's' Output : ['bessst', 'geekforgeekss', 'geeks', 'is', 'for'] Explanation : bessst has 3 occurrence, geeksforgeekss has 3, and so on. I 4 min read