Split List Into Sublists in Python

This tutorial will discuss splitting a list into sublists in Python.

Split List Into Sublists Using List Comprehensions and List Slicing in Python

In Python, list comprehension can create new lists from existing iterables like lists, tuples, etc. List Slicing can access a range of list elements in Python.

We can use list slicing and list comprehensions to create multiple new small lists from a previously existing single large list. We only need to specify the length of all the small lists.

With this method, if the remaining elements are less than the specified length, they automatically get pushed into the last small list. The following code snippet demonstrates the working implementation of this method in Python.

``````original_data = ['This', 'is', 'the', 'original', 'list', 'of', 'data', 'or', 'the', 'original', 'data', 'list']
split_lists = [original_data[x:x+3] for x in range(0, len(original_data), 3)]
print(split_lists)
``````

Output:

``````[['This', 'is', 'the'], ['original', 'list', 'of'], ['data', 'or', 'the'], ['original', 'data', 'list']]
``````

We created a list of 12 elements in `original_data`, split it into smaller lists of length 3, and stored the resulting lists into `split_lists`. We used `x:x+3` as the range of values for list slicing, where `x` is a value between 0 and the total length of the `original_data` list.

The variable `x` gets incremented by 3 because we have specified it in the `range()` function.

In the output, we get 4 small lists of 3 elements.

Split List Into Sublists Using the `array_split()` Function in NumPy

The `array_split()` method in the NumPy library can also split a large array into multiple small arrays.

This function takes the original array and the number of chunks we need to split the array into and returns the split chunks. This method works fine if we pass a list instead of an array as an input parameter.

The following code snippet demonstrates a working implementation of this approach in Python.

``````import numpy as np
original_data = ['This', 'is', 'the', 'original', 'list', 'of', 'data', 'or', 'the', 'original', 'data', 'list']
pieces = 4
new_arrays = np.array_split(original_data, pieces)
print(new_arrays)
``````

Output:

``````[array(['This', 'is', 'the'], dtype='<U8'), array(['original', 'list', 'of'], dtype='<U8'), array(['data', 'or', 'the'], dtype='<U8'), array(['original', 'data', 'list'], dtype='<U8')]
``````

We initialized a list of 12 elements in `original_data` just like we did in the previous example. We then specified the number of pieces we wanted to divide the list into with the `pieces` variable.

We passed the `original_data` and the `pieces` to the `array_split()` function as input parameters and stored the results into `new_arrays`.

In the end, we printed the values inside the `new_arrays`. The output shows 4 small lists, each containing 3 elements inside.

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.