Examples :
Input: [’The’,’ art’, ’of’,’ programming’] Output: [[’of’], [’ The’, ’art’], [’ programming’]] Input: [’Welcome’,’ to’, ’pythonengineering’] Output: [[’to’], [’ Welcome’], [’pythonengineering’]]
Approach # 1: Python is naive
The naive approach for the above method uses a dictionary and a for loop to traverse the list. At each iteration, it is checked whether the item’s length is in the list or not. If not, it adds the length of the element and the element as a key: value
pair, otherwise the element is simply added to the sublist of values. Finally, we list all the dict values and return it.
|
Exit:
[[ ’of’], [’ The’, ’art’], [’ programming’]]
Approach # 2: defaultdict ()
from collections module
This method takes defaultdict and stores it in the & # 39; group_by_len & # 39; variable. Using a for loop, we store the length of the string as a key and the string with the length of the key as the value. Finally, we list all the group_by_len values and return it.
|
Exit:
[[’of’], [’ The’, ’art’], [’ programming’]]
Approach # 3 : groupby ()
from itertools
The most efficient and simpler way to solve this problem is to use groupby ()
from the itertools module. This is a one line string where we use two variables & # 39; l & # 39; (for length) and & # 39; g & # 39; (group of lines) to go through & # 39; lst & # 39;, grouped by length and finally returning all groups packed into a list.
|
Exit:
[([’of’],), ([’ The’, ’art’], ), ([’programming’] ,)]