AppDividend
Latest Code Tutorials

How to Split List in Python

0

A list can be split based on the size of the chunk defined. Splitting a list into n parts returns a list of n lists containing an equal number of list elements. If the number of lists, n, does not evenly divide into the length of the list being split, then some lists will have one more element than others.

Let’s split the list in the traditional way.

Python list split

To split a list in Python, call the len(iterable) method with iterable as a list to find its length and then floor divide the length by 2 using the // operator to find the middle_index of the list.

list = [11, 18, 19, 21]

length = len(list)

middle_index = length // 2

first_half = list[:middle_index]
second_half = list[middle_index:]

print(first_half)
print(second_half)

Output

[11, 18]
[19, 21]

As you can see from the output that we split the list from the exact half. We used the colon operator(:) to access the first and second half of the split list.

How to split a list into n parts in Python

To split a list into n parts in Python, use the numpy.array_split() function. The np.split() function splits the array into multiple sub-arrays.

The numpy array_split() method returns the list of n Numpy arrays, each containing approximately the same number of elements from the list.

import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split(listA, 3)

for array in splits:
    print(list(array))

Output

[11, 18]
[19, 21]
[29, 46]

In this example, we split the list into 3 parts.

Split a List Into Even Chunks of N Elements in Python

A list can be split based on the size of the chunk defined. This means that we can determine the size of the chunk. If the subset of a list doesn’t fit in the size of the defined chunk, fillers need to be inserted in the place of the empty element holders. We will be using None as filters to fill in those empty element holders.

def list_split(listA, n):
    for x in range(0, len(listA), n):
        every_chunk = listA[x: n+x]

        if len(every_chunk) < n:
            every_chunk = every_chunk + \
                [None for y in range(n-len(every_chunk))]
        yield every_chunk


print(list(list_split([11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7)))

Output

[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]

The list has been split into equal chunks of 7 elements each.

The above list_split() function takes the arguments: listA for the list and chunk_size for a number to split it by. The function iterates through the list with an increment of the chunk size n.

Each chunk is expected to have the size given as an argument. If there aren’t enough elements to split the same size, the remaining unused elements are filled with None.

That is it for splitting a list in Python.

See also

Split string with multiple parameters

Split line in Python

Python String rsplit()

Python String splitlines()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.