{"id":14932,"date":"2021-09-30T16:30:17","date_gmt":"2021-09-30T11:00:17","guid":{"rendered":"https:\/\/python-programs.com\/?p=14932"},"modified":"2021-11-22T18:34:24","modified_gmt":"2021-11-22T13:04:24","slug":"python-program-to-break-a-list-into-chunks-of-size-n","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-break-a-list-into-chunks-of-size-n\/","title":{"rendered":"Python Program to Break a List into Chunks of Size N"},"content":{"rendered":"

With the help of a few easy examples, we will learn how to break a list into chunks of any specified size N in Python.<\/p>\n

A list is a collection of items like integers, floats, strings, and so on. Lists are mutable data structures, which means that their contents can be updated without changing their identity. Breaking huge lists into smaller parts is a common process used to improve the efficiency of a program.<\/p>\n

Given a list and the number N the task is to break a list into parts of size N in Python.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given List =['hello', 'this', 'is', 'btechgeeks', 'online', 'coding', 'platform']\r\nGiven Numb =2<\/pre>\n

Output:<\/strong><\/p>\n

[['hello', 'this'], ['is', 'btechgeeks'], ['online', 'coding'], ['platform']]<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given list=good morning this is btechgeeks online programming platform for btech geeks\r\nGiven Number =3<\/pre>\n

Output:<\/strong><\/p>\n

[['good', 'morning', 'this'], ['is', 'btechgeeks', 'online'], ['programming', 'platform', 'for'], ['btech', 'geeks']]<\/pre>\n

Program to Break a List into Chunks of Size N in Python<\/h2>\n

Below are the ways to break a list into parts of size N in Python.<\/p>\n