Given a list of strings, write a Python program to split the list into sublists based on string length.
Examples:
Input : ['The', 'art', 'of', 'programming'] Output : [['of'], ['The', 'art'], ['programming']] Input : ['Welcome', 'to', 'neveropen'] Output : [['to'], ['Welcome'], ['neveropen']]
Approach #1 : Naive
A naive approach for the above method uses a dictionary and a for loop to traverse the list. In each iteration, it checks whether the element length is already in the list or not. If not, it adds the element length and element as key:value pair, otherwise, the element is just added to the value sublist. Finally, we make a list of all the values of the dict and return it.
Python3
# Python3 program to Divide list of strings # into sublists based on string length def divideList(lst): dct = {} for element in lst: if len (element) not in dct: dct[ len (element)] = [element] elif len (element) in dct: dct[ len (element)] + = [element] res = [] for key in sorted (dct): res.append(dct[key]) return res # Driver code lst = [ 'The' , 'art' , 'of' , 'programming' ] print (divideList(lst)) |
[['of'], ['The', 'art'], ['programming']]
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach #2 : defaultdict() from collections module
This method uses defaultdict and saves it in a variable ‘group_by_len’. Using a for loop, we save the length of string as key and the string with the key length as its value. Finally, we make a list of all the values of ‘group_by_len’ and return it.
Python3
# Python3 program to Divide list of strings # into sublists based on string length from collections import defaultdict def divideList(lst): group_by_len = defaultdict( list ) for ele in lst: group_by_len[ len (ele)].append(ele) res = [] for key in sorted (group_by_len): res.append(group_by_len[key]) return res # Driver code lst = [ 'The' , 'art' , 'of' , 'programming' ] print (divideList(lst)) |
[['of'], ['The', 'art'], ['programming']]
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Approach #3 : groupby() from itertools module
The most efficient and simplest method to solve the given problem is using groupby() from itertools module. This is a one-liner where we use two variables ‘l'(for length) and ‘g'(group of strings) to traverse through ‘lst’, grouped by length and finally return all groups packed within a list.
Python3
# Python3 program to Divide list of strings # into sub lists based on string length from itertools import groupby def divideList(lst): res = dict ((l, list (g)) for l, g in groupby(lst, key = len )) # Sorting dict by key res = sorted (res.items(), key = lambda kv:(kv[ 0 ], kv[ 1 ])) # Removing key from list of tuple return [el[ 1 :] for el in ( tuple (x) for x in res)] # Driver code lst = [ 'The' , 'art' , 'of' , 'programming' ] print (divideList(lst)) |
[(['of'],), (['The', 'art'],), (['programming'],)]
Approach #4: Using lists, loops and in, not in operators
Python3
# Python3 program to Divide list of strings # into sublists based on string length lst = [ 'The' , 'art' , 'of' , 'programming' ] x = [] for i in lst: if len (i) not in x: x.append( len (i)) x.sort() res = [] for i in x: a = [] for j in lst: if len (j) = = i: a.append(j) res.append(a) print (res) |
[['of'], ['The', 'art'], ['programming']]