# Break a list into chunks of size N in Python

• Difficulty Level : Medium
• Last Updated : 05 Sep, 2022

In this article, we will cover how we split a list into evenly sized chunks in Python.

Below are the methods that we will cover:

### Method 1: Break a list into chunks of size N in Python using yield keyword

The yield keyword enables a function to come back where it left off when it is called again. This is the critical difference from a regular function. A regular function cannot comes back where it left off. The yield keyword helps a function to remember its state. The yield enables a function to suspend and resume while it turns in a value at the time of the suspension of the execution.

## Python3

 `my_list ``=` `[``'geeks'``, ``'for'``, ``'geeks'``, ``'like'``,` `           ``'geeky'``,``'nerdy'``, ``'geek'``, ``'love'``,` `               ``'questions'``,``'words'``, ``'life'``]`   `# Yield successive n-sized` `# chunks from l.` `def` `divide_chunks(l, n):` `    `  `    ``# looping till length l` `    ``for` `i ``in` `range``(``0``, ``len``(l), n): ` `        ``yield` `l[i:i ``+` `n]`   `# How many elements each` `# list should have` `n ``=` `5`   `x ``=` `list``(divide_chunks(my_list, n))` `print` `(x)`

Output:

```[['geeks', 'for', 'geeks', 'like', 'geeky'],
['nerdy', 'geek', 'love', 'questions', 'words'],
['life']]```

### Method 2: Break a list into chunks of size N in Python using a loop

In this example, we are using a loop in python and list slicing that will help us to break a list into chunks.

## Python3

 `my_list ``=` `[``1``, ``2``, ``3``, ``4``, ``5``,` `           ``6``, ``7``, ``8``, ``9``]` `start ``=` `0` `end ``=` `12` `step ``=` `3` `for` `i ``in` `range``(start, end, step):` `    ``x ``=` `i` `    ``print``(my_list[x:x``+``step])`

Output:

```[1, 2, 3]
[4, 5, 6]
[7, 8, 9]```

### Method 2: Break a list into chunks of size N in Python usingList comprehension

It is an elegant way to break a list into one line of code to split a list into multiple lists in Python.

## Python3

 `my_list ``=` `[``1``, ``2``, ``3``, ``4``, ``5``,` `              ``6``, ``7``, ``8``, ``9``]`   `# How many elements each` `# list should have` `n ``=` `4`   `# using list comprehension` `final ``=` `[my_list[i ``*` `n:(i ``+` `1``) ``*` `n] ``for` `i ``in` `range``((``len``(my_list) ``+` `n ``-` `1``) ``/``/` `n )] ` `print` `(final)`

Output:

```[[1, 2, 3, 4], [5, 6, 7, 8], ]
Auxiliary Space: O(1)```

Alternate Implementation :

## Python3

 `l ``=` `[``1``, ``2``, ``3``, ``4``, ``5``, ``6``, ``7``, ``8``, ``9``] ` ` `  `# How many elements each ` `# list should have ` `n ``=` `4` ` `  `# using list comprehension ` `x ``=` `[l[i:i ``+` `n] ``for` `i ``in` `range``(``0``, ``len``(l), n)] ` `print``(x)`

Output:

`[[1, 2, 3, 4], [5, 6, 7, 8], ]`

### Method 3: Break a list into chunks of size N in Python using Numpy

Here, we are using a Numpy.array_split, which splits the array into n chunks of equal size.

## Python3

 `import` `numpy as np`   `arr ``=` `range``(``30``)` `np.array_split(arr, ``6``)`

Output:

```[array([0, 1, 2, 3, 4]),
array([5, 6, 7, 8, 9]),
array([10, 11, 12, 13, 14]),
array([15, 16, 17, 18, 19]),
array([20, 21, 22, 23, 24]),
array([25, 26, 27, 28, 29])]```

### Method 5: Break a list into chunks of size N in Python using itertool

In this example, we will use itertool to slice each array in equal size. we are passing a parameter in the range of 30 and split size of 5.

## Python3

 `from` `itertools ``import` `islice`     `def` `chunk(arr_range, arr_size):` `    ``arr_range ``=` `iter``(arr_range)` `    ``return` `iter``(``lambda``: ``tuple``(islice(arr_range, arr_size)), ())`     `list``(chunk(``range``(``30``), ``5``))`

Output:

```[(0, 1, 2, 3, 4),
(5, 6, 7, 8, 9),
(10, 11, 12, 13, 14),
(15, 16, 17, 18, 19),
(20, 21, 22, 23, 24),
(25, 26, 27, 28, 29)]```

My Personal Notes arrow_drop_up
Recommended Articles
Page :