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
Custom list split in Python
Data analytics throws complex scenarios where the data need to be wrangled and moved around. In this context let's see how we can take a big list and split it into many sublists as per the requirement. In this article we will explore the approaches to achieve this.
Using List Comprehension and zip()
In this approach we use list slicing to get the elements from specific split points. Then we use zip() to create start and end indices for each sublist.
Example
data_list = ['Mon', 'Tue', 'Wed', 6, 7, 'Thu', 'Fri', 11, 21, 4]
# The indexes to split at
split_points = [2, 5, 8]
# Given list
print("Given list : " + str(data_list))
# Split at
print("The points of splitting : ", split_points)
# Perform the split
split_list = [data_list[i: j] for i, j in zip([0] + split_points, split_points + [None])]
# printing result
print("The split lists are : ", split_list)
Running the above code gives us the following result −
Given list : ['Mon', 'Tue', 'Wed', 6, 7, 'Thu', 'Fri', 11, 21, 4] The points of splitting : [2, 5, 8] The split lists are : [['Mon', 'Tue'], ['Wed', 6, 7], ['Thu', 'Fri', 11], [21, 4]]
Using itertools.chain() and zip()
The chain() function makes an iterator that returns elements from the first iterable until it is exhausted. So it marks the points where the splitting happens. Then we use the zip() function to package the result of splitting into sublists.
Example
from itertools import chain
data_list = ['Mon', 'Tue', 'Wed', 6, 7, 'Thu', 'Fri', 11, 21, 4]
# The indexes to split at
split_points = [2, 5, 8]
# Given list
print("Given list : ", str(data_list))
# Split at
print("The points of splitting : ", split_points)
# to perform custom list split
sublists = zip(chain([0], split_points), chain(split_points, [None]))
split_list = list(data_list[i : j] for i, j in sublists)
# printing result
print("The split lists are : ", split_list)
Running the above code gives us the following result −
Given list : ['Mon', 'Tue', 'Wed', 6, 7, 'Thu', 'Fri', 11, 21, 4] The points of splitting : [2, 5, 8] The split lists are : [['Mon', 'Tue'], ['Wed', 6, 7], ['Thu', 'Fri', 11], [21, 4]]
How It Works
Both methods create pairs of start and end indices for slicing. The first method directly combines indices, while the second uses itertools.chain() to create the same pairs. The key insight is using [0] + split_points for start indices and split_points + [None] for end indices.
Comparison
| Method | Imports Required | Readability | Performance |
|---|---|---|---|
| List Comprehension | None | High | Faster |
| itertools.chain() | itertools | Medium | Slightly slower |
Conclusion
Both approaches effectively split lists at custom points using zip() to pair start and end indices. The list comprehension method is simpler and faster, while itertools.chain() offers more flexibility for complex scenarios.
