Welcome, guest | Sign In | My Account | Store | Cart
NOTE: Recipes have moved! Please visit GitHub.com/activestate/code for the current versions.

This function returns a list of n-tuples from a single "flat" list.

Python, 10 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def group(lst, n):
    """group([0,3,4,10,2,3], 2) => [(0,3), (4,10), (2,3)]
    
    Group a list into consecutive n-tuples. Incomplete tuples are
    discarded e.g.
    
    >>> group(range(10), 3)
    [(0, 1, 2), (3, 4, 5), (6, 7, 8)]
    """
    return zip(*[lst[i::n] for i in range(n)]) 

There is probably an easier technique that accomplishes the same thing but I've found this function useful in XML data processing.

7 comments

Kent Johnson 13 years, 2 months ago  # | flag

Solution using a generator. You can do this with a generator and avoid creating the intermediate lists:

def group(lst, n):
  for i in range(0, len(lst), n):
    val = lst[i:i+n]
    if len(val) == n:
      yield tuple(val)

>>> list(group([0,3,4,10,2,3], 2))
[(0, 3), (4, 10), (2, 3)]
>>> list(group(range(10), 3))
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]
Hamish Lawson 13 years, 2 months ago  # | flag

Requires input to be a sequence rather than just an iterable. This requires that the input be a sequence rather than just an iterable. For a generator-based version that can take any iterable (and which doesn't discard incomplete tail items) see my own recipe at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303279.

Brian Quinlan (author) 13 years, 2 months ago  # | flag

Speed comparison. There is a speed vs memory vs flexibility trade-off that needs to be made here (along with the correct semantics regarding the final incomplete tuple). Here are my timings for the 4 functions that have been presented (group2 and batch2 are the alternate implementations suggested in comments). As you can see, I've skewed the test in favor of the iteration implementations by using a fairly large input list and starting with an iterable input.

import timeit

for fn in ['group1', 'group2', 'batch', 'batch2']:
    if fn.startswith('group'):
        call = '%s(list(x), 3)' % fn
    else:
        call = '%s(x, 3)' % fn

    timer = timeit.Timer(
        'for y in %s: pass\n' % call,
        'x = xrange(10000); from __main__ import %s' % fn)
    print timer.timeit(1000)

Results:

5.60028100014
13.2801439762
16.8175079823
19.8542819023

In the code that I designed this function for [len(lst) ~= 1000, list input], my algorithm is more than 10x faster than the next fastest alternative.

Brian Quinlan (author) 13 years, 2 months ago  # | flag

Fastest iterable version.

def group(lst, n):
    """group([0,3,4,10,2,3], 2) => iterator

    Group an iterable into an n-tuples iterable. Incomplete tuples
    are discarded e.g.

    >>> list(group(range(10), 3))
    [(0, 1, 2), (3, 4, 5), (6, 7, 8)]
    """

Timings:

6.01822805405
14.5682420731
2.44970393181 # this version
18.8882629871
21.9563779831

I'm leaving the original recipe here because it is still appropriate if:

o you are starting with a sequence
o you need list output
o the size of your list/iterable is small
Brian Quinlan (author) 13 years, 2 months ago  # | flag

Now with code!

def group3(lst, n):
    """group([0,3,4,10,2,3], 2) => iterator

    Group an iterable into an n-tuples iterable. Incomplete tuples
    are discarded e.g.

    >>> list(group(range(10), 3))
    [(0, 1, 2), (3, 4, 5), (6, 7, 8)]
    """
    return itertools.izip(*[itertools.islice(lst, i, None, n) for i in range(n)])
Chris Smith 5 years, 2 months ago  # | flag

For more arbitrary reshaping of a sequence, see here .

Nguyen Duc Tien 4 years, 4 months ago  # | flag

We can use zip function, the list in conjunction with itself

1-dimension array ---> 2-dimension array

>>> a=1,2,3,4,5,6,7,8,9
>>> zip(a[0::2],a[1::2])
[(1, 2), (3, 4), (5, 6), (7, 8)]

1-dimension array ---> 3-dimension array

>>> a=1,2,3,4,5,6,7,8,9
>>> zip(a[0::3],a[1::3],a[2::3])
[(1, 2, 3), (4, 5, 6), (7, 8, 9)]
Created by Brian Quinlan on Thu, 2 Sep 2004 (PSF)
Python recipes (4591)
Brian Quinlan's recipes (7)

Required Modules

  • (none specified)

Other Information and Tasks