Split List into N Chunks

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Python, we can split a list into smaller lists of equal size using a simple function. This can be useful when we want to process large amounts of data in smaller chunks. In this challenge, you will be asked to write a function that takes a list and an integer n as input and returns a list of n smaller lists, each containing an equal number of elements from the original list.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/lambda_functions("`Lambda Functions`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13600{{"`Split List into N Chunks`"}} python/variables_data_types -.-> lab-13600{{"`Split List into N Chunks`"}} python/numeric_types -.-> lab-13600{{"`Split List into N Chunks`"}} python/lists -.-> lab-13600{{"`Split List into N Chunks`"}} python/tuples -.-> lab-13600{{"`Split List into N Chunks`"}} python/function_definition -.-> lab-13600{{"`Split List into N Chunks`"}} python/lambda_functions -.-> lab-13600{{"`Split List into N Chunks`"}} python/importing_modules -.-> lab-13600{{"`Split List into N Chunks`"}} python/using_packages -.-> lab-13600{{"`Split List into N Chunks`"}} python/standard_libraries -.-> lab-13600{{"`Split List into N Chunks`"}} python/math_random -.-> lab-13600{{"`Split List into N Chunks`"}} python/data_collections -.-> lab-13600{{"`Split List into N Chunks`"}} python/build_in_functions -.-> lab-13600{{"`Split List into N Chunks`"}} end

Split List into N Chunks

Write a Python function called chunk_into_n(lst, n) that takes a list lst and an integer n as input and returns a list of n smaller lists, each containing an equal number of elements from the original list. If the original list cannot be split evenly into n smaller lists, the final chunk should contain the remaining elements.

To solve this problem, you can follow these steps:

  1. Calculate the size of each chunk by dividing the length of the original list by n and rounding up to the nearest integer using the math.ceil() function.
  2. Create a new list of size n using the list() and range() functions.
  3. Use the map() function to map each element of the new list to a chunk of the original list the length of size.
  4. Return the list of smaller lists.

Your function should have the following signature:

def chunk_into_n(lst: list, n: int) -> list:
from math import ceil

def chunk_into_n(lst, n):
  size = ceil(len(lst) / n)
  return list(
    map(lambda x: lst[x * size:x * size + size],
    list(range(n)))
  )
chunk_into_n([1, 2, 3, 4, 5, 6, 7], 4) ## [[1, 2], [3, 4], [5, 6], [7]]

Summary

In this challenge, you have learned how to split a list into smaller lists of equal size using Python. You have also learned how to use the math.ceil() function to round up to the nearest integer and the map() function to apply a function to each element of a list.

Other Python Tutorials you may like