Python | Splitting list on empty string Last Updated : 03 May, 2023 Comments Improve Suggest changes Like Article Like Report Sometimes, we may face an issue in which we require to split a list to list of list on the blank character sent as deliminator. This kind of problem can be used to send messages or can be used in cases where it is desired to have list of list of native list. Let's discuss certain ways in which this can be done. Method #1 : Using index() and list slicing The list slicing can be used to get the sublists from the native list and index function can be used to check for the empty string which can potentially act as a separator. The drawback of this is that it only works for a single split i.e can only divide a list to 2 sublists. Python3 # Python3 code to demonstrate # divide list to siblist on deliminator # using index() + list slicing # initializing list test_list = ['Geeks', 'for', '', 'Geeks', 1, 2] # printing original list print("The original list : " + str(test_list)) # using index() + list slicing # divide list to siblist on deliminator temp_idx = test_list.index('') res = [test_list[: temp_idx], test_list[temp_idx + 1: ]] # print result print("The list of sublist after separation : " + str(res)) Output : The original list : ['Geeks', 'for', '', 'Geeks', 1, 2] The list of sublist after separation : [['Geeks', 'for'], ['Geeks', 1, 2]] Time Complexity : O(N), where N is the length of the input string. Auxiliary Space: O(N) Method #2 : Using itertools.groupby() + list comprehension The problem of the above proposed method can be solved using the groupby function which could divide on all the list breaks given by the empty strings. Python3 # Python3 code to demonstrate # divide list to siblist on deliminator # using itertools.groupby() + list comprehension from itertools import groupby # initializing list test_list = ['Geeks', '', 'for', '', 4, 5, '', 'Geeks', 'CS', '', 'Portal'] # printing original list print("The original list : " + str(test_list)) # using itertools.groupby() + list comprehension # divide list to siblist on deliminator res = [list(sub) for ele, sub in groupby(test_list, key = bool) if ele] # print result print("The list of sublist after separation : " + str(res)) Output : The original list : ['Geeks', '', 'for', '', 4, 5, '', 'Geeks', 'CS', '', 'Portal'] The list of sublist after separation : [['Geeks'], ['for'], [4, 5], ['Geeks', 'CS'], ['Portal']] Approach#3: Using groupby(): This code uses the groupby() function from the itertools module to group the elements in the list based on the empty string. The lambda function passed to the key parameter of the groupby() function returns True for empty strings and False for all other elements. Then, list comprehension is used to create a list of sublists by iterating over the groups of elements and creating a sublist for each group of elements that is not an empty string. Import the groupby() function from the itertools module.Define the input list last.Use the groupby() function to group the elements in lst based on the empty string. The key parameter of the groupby() function takes a lambda function that returns True for empty string and False for all other elements.Use a list comprehension to create a list of sublists by iterating over the groups of elements and creating a sublist for each group of elements that is not an empty string.Print the list of sublists after separation. Python3 from itertools import groupby lst = ['Geeks', 'for', '', 'Geeks', 1, 2] sublists = [list(g) for k, g in groupby(lst, key=lambda x: x == '') if not k] print("The list of sublists after separation:", sublists) OutputThe list of sublists after separation: [['Geeks', 'for'], ['Geeks', 1, 2]] Time Complexity: O(n), where n is the length of the input list. Space Complexity: O(n), where n is the length of the input list, Comment More infoAdvertise with us Next Article Python | Splitting list on empty string manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Practice Tags : python Similar Reads Python | Splitting string list by strings Sometimes, while working with Python strings, we might have a problem in which we need to perform a split on a string. But we can have a more complex problem of having a front and rear string and need to perform a split on them. This can be multiple pairs for split. Let's discuss certain way to solv 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 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 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 | First Non-Empty String in list Sometimes while dealing with data science, we need to handle a large amount of data and hence we may require shorthands to perform certain tasks. We handle the Null values at preprocessing stage and hence sometimes require to check for the 1st valid element. Let's discuss certain ways in which we ca 5 min read Python | Exceptional Split in String Sometimes, while working with Strings, we may need to perform the split operation. The straightforward split is easy. But sometimes, we may have a problem in which we need to perform split on certain characters but have exceptions. This discusses split on comma, with the exception that comma should 4 min read Python | Delimited String List to String Matrix Sometimes, while working with Python strings, we can have problem in which we need to convert String list which have strings that are joined by deliminator to String Matrix by separation by deliminator. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop + split() T 5 min read Split a String by a Delimiter in Python In Python Programming, the split() method is used to split a string into a list of substrings which is based on the specified delimiter. This method takes the delimiter as an argument and returns the list of substrings. Along with this method, we can use various approaches wot split a string by the 2 min read Split String of list on K character in Python 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 spli 2 min read Split and join a string in Python The goal here is to split a string into smaller parts based on a delimiter and then join those parts back together with a different delimiter. For example, given the string "Hello, how are you?", you might want to split it by spaces to get a list of individual words and then join them back together 3 min read Like