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.
The original list : [‘Geeks’, ‘for’, ”, ‘Geeks’, 1, 2]
The list of sublist after separation : [[‘Geeks’, ‘for’], [‘Geeks’, 1, 2]]
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.
The original list : [‘Geeks’, ”, ‘for’, ”, 4, 5, ”, ‘Geeks’, ‘CS’, ”, ‘Portal’]
The list of sublist after separation : [[‘Geeks’], [‘for’], [4, 5], [‘Geeks’, ‘CS’], [‘Portal’]]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.