Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Categorize the given list by string size
Let's consider a list containing many strings of different lengths. In this article we will see how to group those elements into categories where the strings are of equal length in each group.
Using For Loop
We design a for loop which will iterate through every element of the list and append it only to the group where its length matches with the length of existing elements ?
Example
days = ['Monday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
# Given list
print("Given list:")
print(days)
# Categorize by string size
len_comp = lambda x, y: len(x) == len(y)
result = []
for day in days:
# Find existing group with same length
group = next((g for g in result if len_comp(day, g[0])), [])
if group == []:
result.append(group)
group.append(day)
# Result
print("The list after creating categories:")
print(result)
Given list: ['Monday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] The list after creating categories: [['Monday', 'Friday', 'Sunday'], ['Thursday', 'Saturday']]
Using sort() and groupby()
In this approach we first sort all the elements by their length and then apply the groupby() function from the itertools module ?
Example
from itertools import groupby
days = ['Monday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
# Given list
print("Given list:")
print(days)
# Categorize by string size
get_len = lambda x: len(x)
sorted_days = sorted(days, key=get_len)
result = [list(group) for length, group in groupby(sorted_days, get_len)]
# Result
print("The list after creating categories:")
print(result)
Given list: ['Monday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] The list after creating categories: [['Monday', 'Friday', 'Sunday'], ['Thursday', 'Saturday']]
Using Dictionary Approach
We can also use a dictionary where the key is the string length and the value is a list of strings with that length ?
Example
days = ['Monday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
# Given list
print("Given list:")
print(days)
# Categorize by string size using dictionary
length_groups = {}
for day in days:
length = len(day)
if length not in length_groups:
length_groups[length] = []
length_groups[length].append(day)
# Convert to list of lists
result = list(length_groups.values())
# Result
print("The list after creating categories:")
print(result)
Given list: ['Monday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] The list after creating categories: [['Monday', 'Friday', 'Sunday'], ['Thursday', 'Saturday']]
Comparison
| Method | Time Complexity | Best For |
|---|---|---|
| For Loop | O(n²) | Small lists |
| sort() + groupby() | O(n log n) | Clean, readable code |
| Dictionary | O(n) | Best performance |
Conclusion
The dictionary approach is most efficient with O(n) complexity. Use groupby() for clean readable code, or the for loop approach for simple cases with small datasets.
