# Break a list into chunks of size N in Python

## Break a list into chunks of size N in Python

Method 1: Using yield
The yield keyword enables a function to comeback 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.

 `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: Using List comprehension
List comprehension is an elegant way to break a list in one line of code.

 `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], [9]]
```

Alternate Implementation :

 `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], [9]]
```

Last Updated on October 28, 2021 by admin

## Create empty dataframe in PandasCreate empty dataframe in Pandas

The Pandas Dataframe is a structure that has data in the 2D format and labels

## Python isupper() methodPython isupper() method

Python isupper() method   In Python, isupper() is a built-in method used for string handling.

## How to calculate the Percentage of a column in Pandas ?How to calculate the Percentage of a column in Pandas ?

Pandas is a popular data manipulation library used in Python for performing various data analysis

## Python – Matplotlib.pyplot ticksPython – Matplotlib.pyplot ticks

Python | Matplotlib.pyplot ticks Matplotlib is an amazing visualization library in Python for 2D plots of

## Matplotlib.pyplot.xticks() in PythonMatplotlib.pyplot.xticks() in Python

Matplotlib.pyplot.xticks() in Python Matplotlib is a library in Python and it is numerical – mathematical extension

## Print lists in Python (4 Different Ways)Print lists in Python (4 Different Ways)

Print lists in Python (All Different Ways) Using for loop : Traverse from 0 to

## PUT method – Python requestsPUT method – Python requests

PUT method – Python requests Requests library is one of the important aspects of Python