ActiveState Code

Recipe 576785: Partition an iterable into n lists


This could also be easily modified to return n iterators, but was outside of my needs. Handy for splitting up the workload for use with multiple threads/processes.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def divide(iterable, parts):
    ''' Partitions an iterable into parts number of lists. '''
    items = list(iterable)

    seqs = [[] for _ in xrange(parts)]
    while items:
        for i in xrange(parts):
            if not items:
                break
        
            seqs[i].append(items.pop())
        
    return seqs

Comments

  1. 1. At 12:10 p.m. on 31 may 2009, Matteo Dell'Amico said:

    One-liner using itertools:

    (islice(it, i, None, parts) for i, it in enumerate(tee(iterable, parts)))
    

    or, to generate the lists in a non-lazy way:

    [list(islice(it, i, None, parts)) for i, it in enumerate(tee(iterable, parts))]
    
  2. 2. At 2:10 a.m. on 2 jun 2009, Matteo Dell'Amico said:

    even more compact version returining a list of lists:

    items = list(iterable)
    return [items[i::parts] for i in range(parts)]
    

Sign in to comment