The best and most efficient way to split a list in Python is to use list slicing. First, calculate the length of the list, determine its middle index, and then divide it into two halves.
# Define a list of numbers
main_list = [11, 18, 19, 21, 6, 9, 10, 24]
# Calculate the length of the list
main_list_length = len(main_list)
# Define the middle index for splitting the list
middle_index = main_list_length // 2
# Split the list into two halves
first_part = main_list[:middle_index]
second_part = main_list[middle_index:]
print("first part of the list : ", first_part)
print("second part of the list : ", second_part)
# Output:
# first part of the list : [11, 18, 19, 21]
# second part of the list : [6, 9, 10, 24]
And we have two splitted lists.
Dividing a list involves breaking it down into smaller sublists or elements based on specific criteria, such as size, condition, or delimiters.
Splitting a list into N Equal Parts
You can use list comprehension to break a list into chunks of size N.
main_list = [11, 18, 19, 21, 6, 9, 10, 24]
print("Before Splitting List:", main_list)
# Output: Before Splitting List: [11, 18, 19, 21, 6, 9, 10, 24]
chunk_size = 3
# Use list comprehension to create chunks of size N
split_parts = [main_list[i:i + chunk_size]
for i in range(0, len(main_list), chunk_size)]
print("After Splitting List:", split_parts)
# Output: After Splitting List: [[11, 18, 19], [21, 6, 9], [10, 24]]
Splitting by Delimiter
To split a list based on a delimiter or separator, you can use the itertools.groupby() method.
Let’s say we have a list of numbers that includes 0s. We are choosing 0 as our delimiter or separator and splitting the list from 0. So, if 0 appears multiple times, we have multiple chunks in the output.
from itertools import groupby
lst = [1, 2, 0, 3, 4, 0, 5]
# We are choosing 0 as the delimiter to group the list.
groups = [list(group)
for key, group in groupby(lst, key=lambda x: x != 0) if key]
print(groups)
# Output: [[1, 2], [3, 4], [5]]
You can see that the output does not contain 0 because that acts as a separator, and it won’t be included in the final list of lists.
Splitting by Condition
What if we want to split a list based on even and odd numbers? That means we must divide the list into two groups. The first group of the list contains even numbers, and the second group of the list contains odd numbers.
We will use list comprehension for this type of condition.
lst = [1, 2, 3, 4, 5, 6]
evens = [x for x in lst if x % 2 == 0]
odds = [x for x in lst if x % 2 != 0]
print("Evens:", evens)
# Output: Evens: [2, 4, 6]
print("Odds:", odds)
# Output: Odds: [1, 3, 5]
Splitting a numpy array
A numpy array differs from a Python list. To split the array, we must convert the list into an array using the np.array() method and then use numpy.array_split() method. It splits the array into equal-sized chunks.
import numpy as np
# Define the list of numbers
main_list = [11, 18, 19, 21, 6, 9, 10, 24]
print("Before Splitting List:", main_list)
# Output: Before Splitting List: [11, 18, 19, 21, 6, 9, 10, 24]
# Convert the list to a NumPy array
main_array = np.array(main_list)
# Split the NumPy array into 3 equal-sized chunks (as equal as possible)
chunked_array = np.array_split(main_array, 3)
# Convert each chunked array to a list of Python ints
split_parts = [[int(x) for x in chunk] for chunk in chunked_array]
# Print the list of chunks
print("After Splitting List:", split_parts)
# Output: After Splitting List: [[11, 18, 19], [21, 6], [9, 10, 24]]
That’s all!




Maciek
I really like the second way with the function. Thanks for the clarification, it seems so obvious now!