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 - Group contiguous strings in List
When working with mixed lists containing both strings and numbers, you might need to group contiguous string elements together while keeping non-string elements separate. Python's itertools.groupby() function combined with a custom key function provides an elegant solution.
Understanding the Problem
Given a list with mixed data types, we want to group consecutive string elements into sublists while leaving non-string elements as individual items ?
Solution Using groupby()
We'll create a helper function to identify strings and use groupby() to group contiguous elements ?
from itertools import groupby
def string_check(elem):
return isinstance(elem, str)
def group_string(my_list):
for key, grp in groupby(my_list, key=string_check):
if key:
yield list(grp)
else:
yield from grp
my_list = [52, 11, 'py', 'th', 'on', 11, 52, 'i', 's', 18, 'f', 'un', 99]
print("The list is :")
print(my_list)
my_result = [*group_string(my_list)]
print("The result is:")
print(my_result)
The list is : [52, 11, 'py', 'th', 'on', 11, 52, 'i', 's', 18, 'f', 'un', 99] The result is: [52, 11, ['py', 'th', 'on'], 11, 52, ['i', 's'], 18, ['f', 'un'], 99]
How It Works
The string_check() function returns True for string elements and False for others. The groupby() function groups consecutive elements that return the same key value ?
-
string_check() − Identifies if an element is a string using
isinstance() - groupby() − Groups consecutive elements with the same key (string or non-string)
- yield list(grp) − Returns string groups as lists
- yield from grp − Returns non-string elements individually
Alternative Approach Using Manual Iteration
Here's a more explicit approach without using groupby() ?
def group_strings_manual(data):
result = []
current_group = []
for item in data:
if isinstance(item, str):
current_group.append(item)
else:
if current_group:
result.append(current_group)
current_group = []
result.append(item)
# Add remaining group if exists
if current_group:
result.append(current_group)
return result
my_list = [52, 11, 'py', 'th', 'on', 11, 52, 'i', 's', 18, 'f', 'un', 99]
result = group_strings_manual(my_list)
print(result)
[52, 11, ['py', 'th', 'on'], 11, 52, ['i', 's'], 18, ['f', 'un'], 99]
Comparison
| Method | Code Length | Readability | Performance |
|---|---|---|---|
groupby() |
Shorter | More concise | Efficient |
| Manual iteration | Longer | More explicit | Similar |
Conclusion
Use itertools.groupby() with a key function to efficiently group contiguous string elements in mixed-type lists. The yield approach creates a memory-efficient generator that can handle large datasets.
