By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,995 Members | 1,217 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,995 IT Pros & Developers. It's quick & easy.

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

P: n/a
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]]
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Jul 18 '05 #2

P: n/a
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" <jc*@iteris.com> wrote in message
news:92**************************@posting.google.c om...
| 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]]
Jul 18 '05 #3

P: n/a

"MetalOne" <jc*@iteris.com> wrote in message
news:92**************************@posting.google.c om...
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


Jul 18 '05 #4

P: n/a
Thanks, just what I was looking for.
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.