Python | Categorize the given list by string size

Sometimes, we have a use case in which we need to perform the grouping of strings by various factors, like first letter or any other factor. These type of problems are typical to database queries and hence can occur in web development while programming. This article focuses on one such grouping by size of string. Let’s discuss certain ways in which this can be performed.

Method #1 : Using next() + lambda + loop

The combination of above 3 functions is used to solve this particular problem by the naive method. The lambda function performs the task of finding like lengths, and next function helps in forward iteration.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Categorize by string size 
# using next() + lambda + loop
  
# initializing list
test_list = ['man', 'a', 'geek', 'for', 'b', 'free']
  
# printing original list
print("The original list : " + str(test_list))
  
# using next() + lambda + loop
# Categorize by string size 
util_func = lambda x, y: len(x) == len(y)
res = []
for sub in test_list:
    ele = next((x for x in res if util_func(sub, x[0])), [])
    if ele == []:
        res.append(ele)
    ele.append(sub)
  
# print result
print("The list after Categorization : " + str(res))

chevron_right


Output :

The original list : ['man', 'a', 'geek', 'for', 'b', 'free']
The list after Categorization : [['man', 'for'], ['a', 'b'], ['geek', 'free']]

 

Method #2 : Using sorted() + groupby()

This particular task can also be solved using the groupby function which offers a convenient method to solve this problem. The sorted function sorts the elements by size to be feed to groupby for the relevant grouping.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Categorize by string size 
# using sorted() + groupby()
from itertools import groupby
  
# initializing list
test_list = ['man', 'a', 'geek', 'for', 'b', 'free']
  
# printing original list
print("The original list : " + str(test_list))
  
# using sorted() + groupby()
# Categorize by string size 
util_func = lambda x: len(x)
temp = sorted(test_list, key = util_func)
res = [list(ele) for i, ele in groupby(temp, util_func)]
  
# print result
print("The list after Categorization : " + str(res))

chevron_right


Output :

The original list : ['man', 'a', 'geek', 'for', 'b', 'free']
The list after Categorization : [['a', 'b'], ['man', 'for'], ['geek', 'free']]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.