Split list into chunks | Challenge

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-13069{{"`Split list into chunks | Challenge`"}} python/variables_data_types -.-> lab-13069{{"`Split list into chunks | Challenge`"}} python/lists -.-> lab-13069{{"`Split list into chunks | Challenge`"}} python/tuples -.-> lab-13069{{"`Split list into chunks | Challenge`"}} python/function_definition -.-> lab-13069{{"`Split list into chunks | Challenge`"}} python/lambda_functions -.-> lab-13069{{"`Split list into chunks | Challenge`"}} python/importing_modules -.-> lab-13069{{"`Split list into chunks | Challenge`"}} python/using_packages -.-> lab-13069{{"`Split list into chunks | Challenge`"}} python/standard_libraries -.-> lab-13069{{"`Split list into chunks | Challenge`"}} python/math_random -.-> lab-13069{{"`Split list into chunks | Challenge`"}} python/data_collections -.-> lab-13069{{"`Split list into chunks | Challenge`"}} python/build_in_functions -.-> lab-13069{{"`Split list into chunks | Challenge`"}} end

Split list into chunks

Problem

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.

Example

chunk([1, 2, 3, 4, 5], 2) ## [[1, 2], [3, 4], [5]]
chunk([1, 2, 3, 4, 5], 3) ## [[1, 2, 3], [4, 5]]
chunk([1, 2, 3, 4, 5], 1) ## [[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