• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Split List into N Sublists

Python Split List into N Sublists

February 25, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Replace Forwardslashes in String Using Python
  • 2.  Repeat String with * Operator in Python
  • 3.  Find Median of List in Python
  • 4.  Flatten List of Tuples in Python
  • 5.  Create List of Numbers from 1 to 100 Using Python
  • 6.  Using Python to Count Even Numbers in List
  • 7.  Python os.sep – Create Operating System Path Seperator Character
  • 8.  Python Logging Timestamp – Print Current Time to Console
  • 9.  Using Python to Sum the Digits of a Number
  • 10.  Python Random Boolean – How to Generate Random Boolean Values

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy