-2

I'm trying to create a function that splits a list into n-sized chunks.

For example a function like this: fun([1,2,3,4], 3) should output [[1,2,3],[2,3,4]].

xyres
  • 20,487
  • 3
  • 56
  • 85
honqu
  • 11
  • 1

1 Answers1

0

I'm not sure if this is exactly what you want or not, but I would recommend:

def split_list(list, n):
    toReturn = []
    for i in range(len(list)//n):
        toReturn.append(list[i*n:i*n+n])
    toReturn.append(list[-n:])
    return toReturn
Max
  • 87
  • 11