splitting a list into n groups

Alex Martelli aleax at aleax.it
Thu Oct 9 07:40:24 EDT 2003


Rajarshi Guha wrote:

> Hi,
>   is there an efficient (pythonic) way in which I could split a list into
> say 5 groups? By split I mean the the first x members would be one group,
> the next x members another group and so on 5 times. (Obviously x =
> lengthof list/5)
> 
> I have done this by a simple for loop and using indexes into the list.
> But it does'nt seemm very elegant

from itertools import islice

def split_into_n_groups(alist, n=5):
    it = iter(alist)
    x = len(alist)/n    # note: will just drop the last len(alist) % n items
    for i in range(n):
        yield list(islice(it, x))

print list(split_into_n_groups(range(23)))

emits:

[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18,
19]]

Of course, if you always want to return a list of lists (not a general
sequence -- iterator -- with list items) you can simplify this, e.g.:

def split_into_n_groups(alist, n=5):
    it = iter(alist)
    x = len(alist)/n    # note: will just drop the last len(alist) % n items
    return [ list(islice(it, x)) for i in range(n) ]

and just "print split_into_n_groups(range(23))".


Alex





More information about the Python-list mailing list