In Python, we can split a list into n sublists a number of different ways. Given a length, or lengths, of the sublists, we can use a loop, or list comprehension to split a list into n sublists.

list_of_numbers = [0,1,2,3,4,5]

def getSublists(lst,n):
    subListLength = len(lst) // n
    list_of_sublists = []
    for i in range(0, len(lst), subListLength):
        list_of_sublists.append(lst[i:i+subListLength])
    return list_of_sublists

print(getSublists(list_of_numbers,3))

#Output:
[[0, 1], [2, 3], [4, 5]]

A more efficient way to split a list into sublists is with yield().

list_of_numbers = [0,1,2,3,4,5]

def getSublists(lst,n):
    subListLength = len(lst) // n
    for i in range(0, len(lst), subListLength):
        yield lst[i:i+subListLength]

print(list(getSublists(list_of_numbers,3)))

#Output:
[[0, 1], [2, 3], [4, 5]]

Here is how to split a list into n sublists using list comprehension in Python.

list_of_numbers = [0,1,2,3,4,5]

def getSublists(lst,n):
    subListLength = len(lst) // n 
    return [lst[i:i + subListLength] for i in range(0, len(lst), subListLength)]
    
print(getSublists(list_of_numbers,3))

#Output:
[[0, 1], [2, 3], [4, 5]]

When working with lists in Python, the ability to manipulate them and create new lists is very valuable.

One such manipulation is the ability to split a list into n sublists in Python.

We can use a loop to break a list into n sublists.

To split a list into n sublists, first we need to calculate the length of each sublist. While it might not always work out that there are equal lengths for all n sublists, we can get pretty close.

After getting the length of each sublist, we can then loop over the list and create a list of lists with slicing.

Below is a Python function which will split a list into n sublists with a for loop.

list_of_numbers = [0,1,2,3,4,5]

def getSublists(lst,n):
    subListLength = len(lst) // n
    list_of_sublists = []
    for i in range(0, len(lst), subListLength):
        list_of_sublists.append(lst[i:i+subListLength])
    return list_of_sublists

print(getSublists(list_of_numbers,3))

#Output:
[[0, 1], [2, 3], [4, 5]]

Below shows what will happen if you cannot divide the length of the list by the number of sublists equally.

list_of_numbers = [0,1,2,3,4,5,6]

def getSublists(lst,n):
    subListLength = len(lst) // n
    list_of_sublists = []
    for i in range(0, len(lst), subListLength):
        list_of_sublists.append(lst[i:i+subListLength])
    return list_of_sublists

print(getSublists(list_of_numbers,3))

#Output:
[[0, 1], [2, 3], [4, 5], [6]]

A more efficient way to split a list into sublists is with yield().

list_of_numbers = [0,1,2,3,4,5]

def getSublists(lst,n):
    subListLength = len(lst) // n
    for i in range(0, len(lst), subListLength):
        yield lst[i:i+subListLength]

print(list(getSublists(list_of_numbers,3)))

#Output:
[[0, 1], [2, 3], [4, 5]]

How to Split a List into n Sublists Using List Comprehension in Python

We can also use list comprehension to split a list into n sublists using Python.

To split a list into n sublists, first we need to calculate the length of each sublist. While it might not always work out that there are equal lengths for all n sublists, we can get pretty close.

After getting the length of each sublist, we use list comprehension to loop over the list and creating a list of lists.

Below is a Python function which will split a list into n sublists with list comprehension.

list_of_numbers = [0,1,2,3,4,5]

def getSublists(lst,n):
    subListLength = len(lst) // n 
    return [lst[i:i + subListLength] for i in range(0, len(lst), subListLength)]
    
print(getSublists(list_of_numbers,3))

#Output:
[[0, 1], [2, 3], [4, 5]]

How to Split a List into Evenly Sized Chunks in Python

We can easily modify our function from above and split a list into evenly sized chunks using Python. Instead of calculating the chunk size in the function, we accept it as an argument.

Below is how to split a list into evenly sized chunks using Python.

list_of_numbers = [0,1,2,3,4,5]

def getSublists(lst,chunkSize):
    for i in range(0, len(lst), chunkSize):
        yield lst[i,i + chunkSize]

print(list(getSublists(list_of_numbers,2)))

#Output:
[[0, 1], [2, 3], [4, 5]]

How to Split a List into n Sublists of Different Lengths in Python

The first two examples in this article showed us how to split a list into n sublists where each of the n sublists had an equal, or almost equal length.

You can split a list into n sublist with different lengths with the help of the islice() function from the itertools module.

We can create a function which takes in a list and a list of the lengths you want each sublist to have, and return a list of sublists.

Below is a Python function which will split a list into sublists of given lengths.

from itertools import islice

list_of_numbers = [0,1,2,3,4,5,6,7,8,9,10]

lengths = [3,2,4,2]

def getSublists(lst,lens):
    iter_lst = iter(lst)
    return [list(islice(iter_lst, x)) for x in lens]

print(getSublists(list_of_numbers,lengths))

#Output:
[[0, 1, 2], [3, 4], [5, 6, 7, 8], [9, 10]]

Hopefully this article has been useful for you to learn how to split a list into n sublists using Python.

Categorized in:

Python,

Last Update: February 26, 2024