Is there an easier way to break a list into sub groups

Discussion in 'Python' started by MetalOne, Feb 22, 2004.

  1. MetalOne

    MetalOne Guest

    The following does what I want, but I feel like this could maybe be a
    one liner.
    I just can't think of anything shorter.
    If there is nothing shorter, does this seem like a candidate for
    inclusion in the standard library somewhere.

    >>> def groups(l, n):

    """l is an input list.
    n is the size of the sub group
    returns a list of the sub groups
    """
    .... i=0
    .... g = []
    .... while i < len(l):
    .... g.append(l[i:i+n]) #append sub group to g
    .... i+=n
    .... return g
    ....
    >>> l = [1,2,3,4,5,6]
    >>> groups(l,2)

    [[1, 2], [3, 4], [5, 6]]
    >>> groups(l,3)

    [[1, 2, 3], [4, 5, 6]]
    >>> groups(l,4)

    [[1, 2, 3, 4], [5, 6]]
     
    MetalOne, Feb 22, 2004
    #1
    1. Advertising

  2. MetalOne wrote:

    > The following does what I want, but I feel like this could maybe be a
    > one liner.
    > I just can't think of anything shorter.
    > If there is nothing shorter, does this seem like a candidate for
    > inclusion in the standard library somewhere.
    >
    >>>> def groups(l, n):

    > """l is an input list.
    > n is the size of the sub group
    > returns a list of the sub groups
    > """
    > ... i=0
    > ... g = []
    > ... while i < len(l):
    > ... g.append(l[i:i+n]) #append sub group to g
    > ... i+=n
    > ... return g
    > ...
    >>>> l = [1,2,3,4,5,6]
    >>>> groups(l,2)

    > [[1, 2], [3, 4], [5, 6]]
    >>>> groups(l,3)

    > [[1, 2, 3], [4, 5, 6]]
    >>>> groups(l,4)

    > [[1, 2, 3, 4], [5, 6]]


    It's generally considered more Pythonic to use the range() function instead
    of incrementing counters while looping. Also, list comprehensions are a
    useful tool. These combined give us one possible solution:

    >>> l = range(10)
    >>> n = 2
    >>> [l[i:i+n] for i in range(0, len(l), n)]

    [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]
    >>> n = 3
    >>> [l[i:i+n] for i in range(0, len(l), n)]

    [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
    >>>



    HTH,
    Shalabh
     
    Shalabh Chaturvedi, Feb 22, 2004
    #2
    1. Advertising

  3. MetalOne

    Terry Reedy Guest

    "MetalOne" <> wrote in message
    news:...
    > The following does what I want, but I feel like this could maybe be a
    > one liner.
    > I just can't think of anything shorter.
    > If there is nothing shorter, does this seem like a candidate for
    > inclusion in the standard library somewhere.
    >
    > >>> def groups(l, n):

    > """l is an input list.
    > n is the size of the sub group
    > returns a list of the sub groups
    > """
    > ... i=0
    > ... g = []
    > ... while i < len(l):
    > ... g.append(l[i:i+n]) #append sub group to g
    > ... i+=n
    > ... return g
    > ...
    > >>> l = [1,2,3,4,5,6]
    > >>> groups(l,2)

    > [[1, 2], [3, 4], [5, 6]]
    > >>> groups(l,3)

    > [[1, 2, 3], [4, 5, 6]]
    > >>> groups(l,4)

    > [[1, 2, 3, 4], [5, 6]]


    I believe there was a thread on this very question perhaps 6 months ago.
    Maybe you can find it on Google.

    tjr
     
    Terry Reedy, Feb 22, 2004
    #3
  4. groups = lambda L,n: [L[i*n:(i+1)*n] for i in range(len(L)) if L[i*n:(i+1)*n]]

    HTH


    "MetalOne" <> wrote in message
    news:...
    | The following does what I want, but I feel like this could maybe be a
    | one liner.
    | I just can't think of anything shorter.
    | If there is nothing shorter, does this seem like a candidate for
    | inclusion in the standard library somewhere.
    |
    | >>> def groups(l, n):
    | """l is an input list.
    | n is the size of the sub group
    | returns a list of the sub groups
    | """
    | ... i=0
    | ... g = []
    | ... while i < len(l):
    | ... g.append(l[i:i+n]) #append sub group to g
    | ... i+=n
    | ... return g
    | ...
    | >>> l = [1,2,3,4,5,6]
    | >>> groups(l,2)
    | [[1, 2], [3, 4], [5, 6]]
    | >>> groups(l,3)
    | [[1, 2, 3], [4, 5, 6]]
    | >>> groups(l,4)
    | [[1, 2, 3, 4], [5, 6]]
     
    Elaine Jackson, Feb 22, 2004
    #4
  5. MetalOne

    MetalOne Guest

    Thanks, just what I was looking for.
     
    MetalOne, Feb 22, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Norman Ackroyd
    Replies:
    1
    Views:
    551
  2. dm1608
    Replies:
    13
    Views:
    737
    Kevin Spencer
    Feb 24, 2006
  3. Jim S

    There must be an easier way

    Jim S, Feb 14, 2007, in forum: HTML
    Replies:
    3
    Views:
    404
    John Hosking
    Feb 15, 2007
  4. John Henry
    Replies:
    19
    Views:
    408
    Dennis Lee Bieber
    Dec 1, 2006
  5. Replies:
    19
    Views:
    610
    Ron Adam
    Jul 20, 2007
Loading...

Share This Page