Python Program to Break a List into Chunks of Size N

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.

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.

Given a list and the number N the task is to break a list into parts of size N in Python.

Examples:

Example1:

Input:

Given List =['hello', 'this', 'is', 'btechgeeks', 'online', 'coding', 'platform']
Given Numb =2

Output:

[['hello', 'this'], ['is', 'btechgeeks'], ['online', 'coding'], ['platform']]

Example2:

Input:

Given list=good morning this is btechgeeks online programming platform for btech geeks
Given Number =3

Output:

[['good', 'morning', 'this'], ['is', 'btechgeeks', 'online'], ['programming', 'platform', 'for'], ['btech', 'geeks']]

Program to Break a List into Chunks of Size N in Python

Below are the ways to break a list into parts of size N in Python.

Method #1: Using yield keyword (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Give the Number N as static input and store it in another Variable.
  • Create a function listchunks() that accept the given list and the number as arguments and return the chunks of the given list.
  • Calculate the length of a list using the len() function.
  • Inside the function iterate from 0 to the length of the given list and give the third parameter as n in the range() function using the For loop.
  • Using the yield keyword slice from iterator value to the length of the list.
  • Pass the given list and number N to listchunks() function.
  • Convert this result to the list() and store it in a variable.
  • Print the above result.
  • The Exit of the Program.

Below is the Implementation:

# Create a function listchunks() that accept the given list
# and the number as arguments and return the chunks of the given list.
def listchunks(gvnlst, gvnumb):
        # Calculate the length of a list using the len() function.
    lstleng = len(gvnlst)
    # Inside the function iterate from 0 to the length of the
    # given list and give the third parameter as n in the range() function
    # using the For loop.
    for m in range(0, lstleng, gvnumb):
        # Using the yield keyword slice from iterator value to the length of the list.
        yield gvnlst[m:m + gvnumb]

# Give the list as static input and store it in a variable.
gvnlist = ['hello', 'this', 'is', 'btechgeeks', 'online', 'coding', 'platform']
# Give the Number N as static input and store it in another Variable.
numb = 2
# Pass the given list and number N to listchunks() function.
resllt = listchunks(gvnlist, numb)
# Convert this result to the list() and store it in a variable.
resllt = list(resllt)
# Print the above result.
print(resllt)

Output:

[['hello', 'this'], ['is', 'btechgeeks'], ['online', 'coding'], ['platform']]

So, the yield keyword’s most important distinguishing feature is its ability to return to the position it left at in the previous iteration of a loop. In the preceding example, after the list is separated into a sub-list of 2 objects, the yield keyword allows the function to return and resume from the third position.

Method #2: Using yield keyword (User Input)

Approach:

  • Give the list as user input using the list(), input() functions, and store the list in a variable.
  • Give the Number N as user input using the int(input()) function and store it in another Variable.
  • Create a function listchunks() that accept the given list and the number as arguments and return the chunks of the given list.
  • Calculate the length of a list using the len() function.
  • Inside the function iterate from 0 to the length of the given list and give the third parameter as n in the range() function using the For loop.
  • Using the yield keyword slice from iterator value to the length of the list.
  • Pass the given list and number N to listchunks() function.
  • Convert this result to the list() and store it in a variable.
  • Print the above result.
  • The Exit of the Program.

Below is the Implementation:

# Create a function listchunks() that accept the given list
# and the number as arguments and return the chunks of the given list.


def listchunks(gvnlst, gvnumb):
        # Calculate the length of a list using the len() function.
    lstleng = len(gvnlst)
    # Inside the function iterate from 0 to the length of the
    # given list and give the third parameter as n in the range() function
    # using the For loop.
    for m in range(0, lstleng, gvnumb):
        # Using the yield keyword slice from iterator value to the length of the list.
        yield gvnlst[m:m + gvnumb]


# Give the list as user input using the list(), input() functions,
# and store the list in a variable.
gvnlist = list(
    input('Enter some random string elements for the list = ').split())
# Give the Number N as user input using the int(input()) function
# and store it in another Variable.
numb = int(input('Enter some random number = '))
# Pass the given list and number N to listchunks() function.
resllt = listchunks(gvnlist, numb)
# Convert this result to the list() and store it in a variable.
resllt = list(resllt)
# Print the above result.
print(resllt)

Output:

Enter some random string elements for the list = good morning this is btechgeeks online programming platform for btech geeks
Enter some random number = 3
[['good', 'morning', 'this'], ['is', 'btechgeeks', 'online'], ['programming', 'platform', 'for'], ['btech', 'geeks']]

Related Programs: