Split List Into Smaller Chunks

PythonPythonBeginner
Practice Now

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

Introduction

In Python, we often need to split a list into smaller lists of a specified size. This can be useful for various purposes, such as processing data in batches or displaying data in a paginated format. In this challenge, you will be tasked with writing a function that can split a list into smaller lists of a specified size.


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/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-13601{{"`Split List Into Smaller Chunks`"}} python/variables_data_types -.-> lab-13601{{"`Split List Into Smaller Chunks`"}} python/lists -.-> lab-13601{{"`Split List Into Smaller Chunks`"}} python/tuples -.-> lab-13601{{"`Split List Into Smaller Chunks`"}} python/function_definition -.-> lab-13601{{"`Split List Into Smaller Chunks`"}} python/lambda_functions -.-> lab-13601{{"`Split List Into Smaller Chunks`"}} python/importing_modules -.-> lab-13601{{"`Split List Into Smaller Chunks`"}} python/using_packages -.-> lab-13601{{"`Split List Into Smaller Chunks`"}} python/standard_libraries -.-> lab-13601{{"`Split List Into Smaller Chunks`"}} python/math_random -.-> lab-13601{{"`Split List Into Smaller Chunks`"}} python/data_collections -.-> lab-13601{{"`Split List Into Smaller Chunks`"}} python/build_in_functions -.-> lab-13601{{"`Split List Into Smaller Chunks`"}} end

Split list into chunks

Write a function chunk(lst, size) that takes a list lst and a positive integer size as arguments and returns a list of smaller lists, each of which has a maximum size of size. If the length of lst is not evenly divisible by size, the last list in the returned list should contain the remaining elements.

from math import ceil

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

Summary

In this challenge, you have learned how to split a list into smaller lists of a specified size. You have written a function that takes a list and a positive integer as arguments and returns a list of smaller lists, each of which has a maximum size of the given integer. This can be useful for various purposes, such as processing data in batches or displaying data in a paginated format.

Other Python Tutorials you may like