If the elements are sorted, then you can use groupby as depicted in the code

import itertools input=[0,1,5,17,18,22,27,37,39,40,41,48,57,65,68,72,77,79,81,85,88,91,99]

for i, j in itertools.groupby(input, key=lambda x: x//20):

# i=0, j=[0, 1, 5, 17, 18]

# i=1, j=[22, 27, 37, 39]

# i=2, j=[40, 41, 48, 57]

Otherwise

You should use **np.searchsorted()** to get the indices that will split into groups.

After that, split those with **np.split **-

np.random.seed(0)

a = np.sort(np.random.randint(0,100,(10000)))

bins = [20,40,60,80]

idx = np.searchsorted(a, bins)

np.split(a,idx)

Output:

[array([ 0, 0, 0, ..., 19, 19, 19]),

array([20, 20, 20, ..., 39, 39, 39]),

array([40, 40, 40, ..., 59, 59, 59]),

array([60, 60, 60, ..., 79, 79, 79]),

array([80, 80, 80, ..., 99, 99, 99])]

If you want to learn data science in-depth then enroll for best data science training.