Splitting Lists into Sub-Lists in Python: Techniques and Advantages

Split A Python List Into Other Sub Lists

The ‘list’ is a basic part of the Python language. The list is implemented in almost every deep learning, machine learning, and data science model, where Python is used as the main language. We can perform various operations on the Python lists. In this article, we will explore different techniques to split a list into sub-lists ( smaller lists). Python language provides different functions, libraries, and packages to implement these techniques elegantly and easily. So, let’s see a few techniques with examples.

Techniques for Splitting Lists into Sub-Lists

Python language has rich support for libraries, packages, and functions. We can use functions like zip(), xrange() and isslice to split a list into ‘n’ numbers of sub-lists. These methods are simple and easy to use. Let’s see an implementation to understand all methods in detail.

Using the islice() Function

The isslice() function is available in the itertools module, which deals with iterables such as List. The isslice() function in Python splits a list into sub-lists. We can directly use this function with a loop to implement the example. In this method, we must provide a step in which the list is divided. Depending on the size of the steps, we are dividing the list into sub-lists. Let’s see an implementation to understand the methods.

from itertools import islice

def split_list(lst, step):
    sublists = []
    for i in range(0, len(lst), step):
        sublists.append(list(islice(lst, i, i+step)))
    return sublists

example_list = [1, 3, 4, 5, 6, 2, 3, 8, 0, 3, 7]
step_size = 4
result_set = split_list(example_list, step_size)
print(result_set)

In this example, we have set the step size as 4. So the total number of elements in a particular list will be 4. The remaining elements will be displayed together in a last sublist. The loop is used to iterate through the list. The starting index of the list is 0 in this case. Let’s see an outcome for better understanding.

Isslice Function In Python
Isslice Function In Python

Using xrange() Along With split_list() Function

We can’t directly utilize the xrange() function to split the list into sublists. To split the list we can use a combination of the xrange() and split_list() functions in Python. In this method, the split_list() function helps to take an index and the step size as an argument and the xrange() function helps to iterate through the list. Let’s see a code to understand this method.

def split_list(lst, step):
    sublists = []
    for i in xrange(0, len(lst), step):
        sublists.append(lst[i:i+step])
    return sublists

example_list = [1, 3, 4, 5, 6, 2, 3, 8, 0, 3, 7]
step_size = 4
result_set = split_list(example_list, step_size)
print(result_set)

We have set the step size of 4 here. So, every sublist will hold four elements. This method is valid in the Python2 version. In Python3, we can use the range() function instead of xrange().

Xrange With Split List Function
Xrange With Split List Function

Using the zip() Function

The zip() function in Python is used to pair the elements from the input iterables. The zip() function is also used to split the list into sublists. The combination of the split_list() function and zip() function will convert a list into sub-lists. The zip() function will help to pair the elements.

def split_list(lst, chunk_size):
    return [list(sublist) for sublist in zip(*[iter(lst)]*chunk_size)]

my_list = [1, 3, 4, 5, 6, 2, 3, 8, 0, 3, 7]
chunk_size = 4
result_set = split_list(my_list, chunk_size)
print(result_set)

Again the step_size is 4 for this example. Let’s see the implementation for this example.

Zip Function In Python
Zip Function In Python

Advantages of Converting Lists into Sub-Lists

1. Handling Smaller Chunks of Data

Occasionally, working with data tables or large data flow may be more easy to handle on the smaller portions, than on all of the big chunks at a time. Divide data into sub lists help you to run the process in portions that you can handle.

2. Enabling Parallel Processing

The subdivision of a task into smaller subcomponents, which depending on architecture, could be executed across multiple processors (CPUs) or threads at the same time is the approach used in parallel computing. The process of partitioning a list into smaller sub-lists is good because it is possible to assign individual subtasks to various processors and process the sub-lists simultaneously.

3. Facilitating Batch Processing

If you use machine learning or data analysis, you should consider working with batches instead of the entire dataset to speed up execution. Splitting a list into sub-lists lets you work with the data in batches and thus, speed up the program response.

4. Grouping Data Sets

For instance, dividing a list into sub-lists based on whether or not they meet particular conditions enables you to assemble the associated data elements.

Summary

This article has shown different techniques for splitting the list into sub-lists. The Python language provides various modules and packages to implement this technique with ease. Here, we have implemented 3 techniques with isslice(), xrange(), and zip() function. The detailed implementation of all three functions, along with examples, is also explained. The advantages of this technique are also explained. Hope you will enjoy this article.

References

Stack Overflow Query.