How to divide list in python

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: 

  • Using yield
  • Using for loop in Python
  • Using List comprehension
  • Using Numpy
  • Using itertool

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']

def divide_chunks(l, n):

    for i in range(0, len(l), n):

        yield l[i:i + n]

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 using List 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]

n = 4

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]]
Auxiliary Space: O(1)

Alternate Implementation : 

Python3

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

n = 4

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]]

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)]

How do you divide a list of numbers in Python?

Create an empty list that will store the quotients. Iterate across all the elements in the given list using a for loop. Divide each element with the given number/divisor and append the result in the resultant list. Finally, display the resultant list after all the quotients have been calculated and appended to it.

How do you split a list in a list Python?

To split the elements of a list in Python: Use a list comprehension to iterate over the list. On each iteration, call the split() method to split each string. Return the part of each string you want to keep.

How do you split a list in half in Python?

This can be done using the following steps:.
Get the length of a list using len() function..
If the length of the parts is not given, then divide the length of list by 2 using floor operator to get the middle index of the list..
Slice the list into two halves using [:middle_index] and [middle_index:].

How do you divide a list into equal parts?

The easiest way to split list into equal sized chunks is to use a slice operator successively and shifting initial and final position by a fixed number.