4 Easy Ways to Split List in Python

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.

Python list split

4 easy ways to split a list in Python.

  1. Using len(): The len() method returns the length of the list and then use the floor divide.
  2. Using list comprehension: Split the list into chunks, and provide the N to the list comprehension.
  3. Using for loop: Use a for loop to split the list into different chunks.
  4. Using numpy array_split(): It allows you to split an array into a set number of arrays.

Method 1: Using the len() method

To split a list in Python, use the len() 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, we split the list in 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.

Splitting 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 the size of the defined chunk, fillers need to be inserted in place of the empty element holders.

Therefore, we will use None as a filter to fill 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 by. Then, 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.

Method 2: Using list comprehension

Using list comprehension, we can split an original list and create chunks. To split the list into chunks, provide the N to the list comprehension syntax, and it will create a new list with N chunks of elements.

main_list = [11, 21, 46, 19]

split_size = 3
splitted_list = [main_list[i:i+split_size] for i in range(0, len(main_list), split_size)]

print(splitted_list)

Output

[[11, 21, 46], [19]]

You can see that we split a list into chunks of 3 elements. You can divide the list into chunks of 2 elements or four elements. Pass the split_size, which is N in our case.

Method 3: Using for loop

Use a for loop to split the list into different chunks. Define the chunk size of the list, and it will create a list of chunks of that exact size.

main_list = [11, 21, 46, 19]

chunk_size = 2
splitted_list = list()

for i in range(0, len(main_list), chunk_size):
splitted_list.append(main_list[i:i+chunk_size])

print(splitted_list)

Output

[[11, 21], [46, 19]]

Method 4: Using numpy array_split()

The np.array_split() is a numpy library function that splits the list into chunks. It allows you to split an array into a set number of arrays.

import numpy as np

main_list = [11, 21, 46, 19]
main_array = np.array(main_list)

chunked_array = np.array_split(main_list, 2)
splitted_list = [list(array) for array in chunked_array]

print(splitted_list)

Output

[[11, 21], [46, 19]]

FAQ

How to split a list into multiple lists in Python?

Use list slicing to split a list into multiple lists.

main_list = [11, 21, 19, 18]

chunk_1 = main_list[:2]
chunk_2 = main_list[2:]

print(chunk_1)
print(chunk_2)

Output

[11, 21]
[19, 18]

How to split a list into a specific number of sublists in Python?

Use a list slicing in combination with the range() function to split a list into a specific number of sublists.

main_list = [11, 21, 19, 18]

chunk_size = len(main_list)//3

sublists = [main_list[i:i+chunk_size]
 for i in range(0, len(main_list), chunk_size)]

print(sublists)

Output

[[11], [21], [19], [18]]

Conclusion

To split the length of a list in Python, you can use the built-in len() function in combination with floor divide the length by 2 using the // operator to find the middle_index of the list.

Apart from the len() function approach, you can also use a for loop, list comprehension, or numpy array_split() function.

Further reading

Split string with multiple parameters

Split line in Python

Python String splitlines()

1 thought on “4 Easy Ways to Split List in Python”

Leave a Comment

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