Split List into N Chunks | Challenge

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/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) 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/ErrorandExceptionHandlingGroup -.-> python/custom_exceptions("`Custom Exceptions`") 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/variables_data_types -.-> lab-13068{{"`Split List into N Chunks | Challenge`"}} python/numeric_types -.-> lab-13068{{"`Split List into N Chunks | Challenge`"}} python/lists -.-> lab-13068{{"`Split List into N Chunks | Challenge`"}} python/tuples -.-> lab-13068{{"`Split List into N Chunks | Challenge`"}} python/function_definition -.-> lab-13068{{"`Split List into N Chunks | Challenge`"}} python/lambda_functions -.-> lab-13068{{"`Split List into N Chunks | Challenge`"}} python/importing_modules -.-> lab-13068{{"`Split List into N Chunks | Challenge`"}} python/using_packages -.-> lab-13068{{"`Split List into N Chunks | Challenge`"}} python/standard_libraries -.-> lab-13068{{"`Split List into N Chunks | Challenge`"}} python/custom_exceptions -.-> lab-13068{{"`Split List into N Chunks | Challenge`"}} python/math_random -.-> lab-13068{{"`Split List into N Chunks | Challenge`"}} python/data_collections -.-> lab-13068{{"`Split List into N Chunks | Challenge`"}} python/build_in_functions -.-> lab-13068{{"`Split List into N Chunks | Challenge`"}} end

Split List into N Chunks

Problem

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:

Example

assert chunk_into_n([1, 2, 3, 4, 5, 6, 7], 4) == [[1, 2], [3, 4], [5, 6], [7]]
assert chunk_into_n([1, 2, 3, 4, 5, 6, 7], 3) == [[1, 2, 3], [4, 5, 6], [7]]
assert chunk_into_n([1, 2, 3, 4, 5, 6, 7], 2) == [[1, 2, 3, 4], [5, 6, 7]]
assert chunk_into_n([1, 2, 3, 4, 5, 6, 7], 1) == [[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