Yet another variation of splitting strings is splitting the strings that are an element of the sublist. This is quite a peculiar problem, but one can get the data in this format, and the knowledge of splitting it anyways is quite useful. Let’s discuss certain ways in which this particular task can be performed.
Method #1 : Using list comprehension + split()
This method is the shorthand version of the longer loop version that one could choose to solve this particular problem. We just split the strings fetching the sublist using the loop in list comprehension using the split function.
Python3
# Python3 code to demonstrate # Split Sublist Strings # using split() + list comprehension # initializing list test_list = [[ 'GfG is best' ], [ 'All love Gfg' ], [ 'Including me' ]] # printing original list print ( "The original list : " + str (test_list)) # using split() + list comprehension # Split Sublist Strings res = [sub.split() for subl in test_list for sub in subl] # print result print ( "The list after splitting strings : " + str (res)) |
The original list : [['GfG is best'], ['All love Gfg'], ['Including me']] The list after splitting strings : [['GfG', 'is', 'best'], ['All', 'love', 'Gfg'], ['Including', 'me']]
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using list comprehension + split() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #2: Using map() + lambda + split()
This task can also be performed using the combination of the above 3 functions. The map function binds the splitting logic to each element which is written using the lambda function that uses split function to perform the split.
Python3
# Python3 code to demonstrate # Split Sublist Strings # using map() + lambda + split() # initializing list test_list = [[ 'GfG is best' ], [ 'All love Gfg' ], [ 'Including me' ]] # printing original list print ( "The original list : " + str (test_list)) # using map() + lambda + split() # Split Sublist Strings res = list ( map ( lambda sub: sub[ 0 ].split( ' ' ), test_list)) # print result print ( "The list after splitting strings : " + str (res)) |
The original list : [['GfG is best'], ['All love Gfg'], ['Including me']] The list after splitting strings : [['GfG', 'is', 'best'], ['All', 'love', 'Gfg'], ['Including', 'me']]
Method #3: Using itertools.chain()
The itertools.chain() method takes an iterable (such as a list) and returns an iterator that returns the elements of the iterable, one after the other. In this case, the * operator is used to unpack the sublists in test_list and pass their elements as individual arguments to chain(), so that the resulting iterator returns the strings in test_list as a flat list.
The split() method is then used to split each of these strings into a list of substrings. The resulting list comprehension returns a list of lists, where each inner list contains the substrings of a single string in test_list.
Python3
# import the itertools module import itertools # initializing list test_list = [[ 'GfG is best' ], [ 'All love Gfg' ], [ 'Including me' ]] # Flattening test_list and split each string # use the chain() method result = [s.split() for s in itertools.chain( * test_list)] # Printing the result print (result) # This code is contributed by Edula Vinay Kumar Reddy |
[['GfG', 'is', 'best'], ['All', 'love', 'Gfg'], ['Including', 'me']]
Time complexity: O(n), where n is the number of elements in test_list. This is because each element in test_list is processed once by chain() and once by split().
Auxiliary space: O(n), for storing results.
Method #4: Using for loop
Python3
test_list = [[ 'GfG is best' ], [ 'All love Gfg' ], [ 'Including me' ]] result = [] for sublist in test_list: for sub in sublist: result.append(sub.split()) print ( "The list after splitting strings:" , result) # This code is contributed by Jyothi pinjala. |
The list after splitting strings: [['GfG', 'is', 'best'], ['All', 'love', 'Gfg'], ['Including', 'me']]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #8: Using a generator expression with split() function
- Create a nested list test_list containing 3 sublists with one string element each.
- Define a generator expression that uses the split() function to split each string element of the nested list into a list of words.
- The generator expression uses a nested loop to iterate through each sublist and string element of the nested list.
- Convert the generator expression to a list using the list() function and assign it to the variable res.
- Print the result list using the print() function and the str() method.
Python3
# initializing list test_list = [[ 'GfG is best' ], [ 'All love Gfg' ], [ 'Including me' ]] # printing original list print ( "The original list : " + str (test_list)) # using a generator expression with split() function res = (sub.split() for subl in test_list for sub in subl) # convert generator object to list res = list (res) # print result print ( "The list after splitting strings : " + str (res)) |
The original list : [['GfG is best'], ['All love Gfg'], ['Including me']] The list after splitting strings : [['GfG', 'is', 'best'], ['All', 'love', 'Gfg'], ['Including', 'me']]
Time complexity: O(n), where n is the number of elements in the input list.
Auxiliary space: O(n), where n is the total number of characters in the strings in the input list.
Method #6 : Using reduce() and split()
Python3
from functools import reduce # initializing list test_list = [[ 'GfG is best' ], [ 'All love Gfg' ], [ 'Including me' ]] # printing original list print ( "The original list: " + str (test_list)) # using reduce() and split() to split strings res = [[word for word in sub[ 0 ].split()] for sub in test_list] # print result print ( "The list after splitting strings: " + str (res)) |
The original list: [['GfG is best'], ['All love Gfg'], ['Including me']] The list after splitting strings: [['GfG', 'is', 'best'], ['All', 'love', 'Gfg'], ['Including', 'me']]
Time Complexity: O(N*M), where N is the number of sublists in the test_list and M is the average number of words in each sublist.
Auxiliary Space: O(K), where K is the total number of words in all sublists combined.