Python | Splitting list on empty string Last Updated : 03 May, 2023 Comments Improve Suggest changes Like Article Like Report See More 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, Create Quiz Comment M manjeet_04 Follow 0 Improve M manjeet_04 Follow 0 Improve Article Tags : Python Python Programs Python list-programs Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like