Categories
chunks list python split

How do I split a list into equally-sized chunks?

2890

How do I split a list of arbitrary length into equal sized chunks?

Related question: How to iterate over a list in chunks

2

  • 27

    Before you post a new answer, consider there are already 60+ answers for this question. Please, make sure that your answer contributes information that is not among existing answers.

    – janniks

    Feb 3, 2020 at 12:17

  • 2

    The string equivalent of this question: Split string every nth character? (while some answers overlap and apply for both, there are some unique for each)

    – Tomerikoo

    Dec 28, 2021 at 15:04

4138

+100

Here’s a generator that yields evenly-sized chunks:

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]
import pprint
pprint.pprint(list(chunks(range(10, 75), 10)))
[[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
 [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
 [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
 [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
 [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
 [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
 [70, 71, 72, 73, 74]]

For Python 2, using xrange instead of range:

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in xrange(0, len(lst), n):
        yield lst[i:i + n]

Below is a list comprehension one-liner. The method above is preferable, though, since using named functions makes code easier to understand. For Python 3:

[lst[i:i + n] for i in range(0, len(lst), n)]

For Python 2:

[lst[i:i + n] for i in xrange(0, len(lst), n)]

17

  • 92

    What happens if we can’t tell the length of the list? Try this on itertools.repeat([ 1, 2, 3 ]), e.g.

    – jespern

    Nov 23, 2008 at 12:51

  • 56

    That’s an interesting extension to the question, but the original question clearly asked about operating on a list.

    Nov 23, 2008 at 13:53

  • 106

    this functions needs to be in the damn standard library

    – dgan

    Feb 4, 2018 at 14:19

  • 12

    @Calimo: what do you suggest? I hand you a list with 47 elements. How would you like to split it into “evenly sized chunks”? The OP accepted the answer, so they are clearly OK with the last differently sized chunk. Perhaps the English phrase is imprecise?

    Jun 14, 2018 at 15:29

  • 11

    Most people will be looking at this for batch processing and rate limiting, so it usually doesn’t matter if the last chunk is smaller

    – Alvaro

    Jul 4, 2019 at 12:46

642

Something super simple:

def chunks(xs, n):
    n = max(1, n)
    return (xs[i:i+n] for i in range(0, len(xs), n))

For Python 2, use xrange() instead of range().

3

  • 7

    Or (if we’re doing different representations of this particular function) you could define a lambda function via: lambda x,y: [ x[i:i+y] for i in range(0,len(x),y)] . I love this list-comprehension method!

    – J-P

    Aug 20, 2011 at 13:54


  • Using short circuiting, len(l) or 1 to deal with empty lists.

    – keepAlive

    Aug 12, 2021 at 15:24


  • # return generator of chunks of size n from list l

    Feb 21 at 16:17

375

I know this is kind of old but nobody yet mentioned numpy.array_split:

import numpy as np

lst = range(50)
np.array_split(lst, 5)

Result:

[array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
 array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),
 array([20, 21, 22, 23, 24, 25, 26, 27, 28, 29]),
 array([30, 31, 32, 33, 34, 35, 36, 37, 38, 39]),
 array([40, 41, 42, 43, 44, 45, 46, 47, 48, 49])]

4

  • 45

    This allows you to set the total number of chunks, not the number of elements per chunk.

    – FizxMike

    Sep 9, 2015 at 3:03

  • 4

    Its not hard to solve for x… : np.array_split(lst, int(len(lst)/5)) to get a list where each sublist’s length is 5 or less.

    Dec 7, 2021 at 8:44

  • using @PéterSzabó-tóth method makes an error if your number get under 0 and thats bad.

    – HadiH2o

    Apr 7 at 19:11

  • 1

    @PéterSzabó-tóth int() rounds the number of chunks downwards, so each chunk will contain 5 or more items. For example, when lst = range(49), you get four chunks of 6 items and five chunks of 5 items.

    Jun 30 at 7:27