Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Split String of list on K character

Improve Article
Save Article
  • Last Updated : 05 Feb, 2023
Improve Article
Save Article

Sometimes while working with data, we can have a problem in which we need to perform split operation on Strings, and sometimes, we might also require to perform them into nested strings overall. Lets discuss certain ways in which this task can be performed. 

Method #1 : Using loop + split() The combination of above functionalities can be used to perform this task. In this, we iterate through each list string and perform a manual split and then add the new elements to that list using extend() using loop. 

Python3




# Python3 code to demonstrate
# Split String of list on K character
# using loop + split()
 
# Initializing list
test_list = ['Gfg is best', 'for Geeks', 'Preparing']
 
# printing original list
print("The original list is : " + str(test_list))
 
K = ' '
 
# Split String of list on K character
# using loop + split()
res = []
for ele in test_list:
    sub = ele.split(K)
    res.extend(sub)
 
# printing result
print ("The extended list after split strings : " + str(res))

Output : 

The original list is : ['Gfg is best', 'for Geeks', 'Preparing']
The extended list after split strings : ['Gfg', 'is', 'best', 'for', 'Geeks', 'Preparing']

  Method #2 : Using join() + split() The combination of above functions can be used to perform this task. In this, we join split all the elements of list and then join each of them to make it split by K. 

Python3




# Python3 code to demonstrate
# Split String of list on K character
# using join() + split()
 
# Initializing list
test_list = ['Gfg is best', 'for Geeks', 'Preparing']
 
# printing original list
print("The original list is : " + str(test_list))
 
K = ' '
 
# Split String of list on K character
# using join() + split()
res = K.join(test_list).split(K)
 
# printing result
print ("The extended list after split strings : " + str(res))

Output : 

The original list is : ['Gfg is best', 'for Geeks', 'Preparing']
The extended list after split strings : ['Gfg', 'is', 'best', 'for', 'Geeks', 'Preparing']

Method #3 : Here is another approach using itertools.chain and str.split:

Python3




import itertools
 
test_list = ['Gfg is best', 'for Geeks', 'Preparing']
K = ' '
 
# Split String of list on K character using itertools.chain and str.split
result = list(itertools.chain(*(s.split(K) for s in test_list)))
 
print("The extended list after split strings:", result)
#This code is contributed by Edula Vinay Kumar Reddy

Output

The extended list after split strings: ['Gfg', 'is', 'best', 'for', 'Geeks', 'Preparing']

This approach uses itertools.chain to concatenate the output of str.split for each element in test_list. The s.split(K) operation returns a list of strings that were split from s using K as the delimiter. The itertools.chain function then concatenates the outputs from all iterations into a single list.

The time complexity of this approach is O(n), where n is the total number of characters in all elements of test_list. The space complexity is O(n) as well, since we need to store the output list in memory.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!