473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
4 1188
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
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

"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
Thanks, just what I was looking for.
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: temp | last post by:
Hi All, I wonder could someone help me with this? What I want to do is search through a list of letters and look for adjacent groups of letters that form sequences, not in the usual way of...
0
by: Mike Cox | last post by:
Andy M wrote: > ALERT > > There is a person by the name of Mike Cox who's trying > to turn this mailing list into a Big-8 newsgroup. No, I'm trying to get teh postgresql groups which are...
13
by: dm1608 | last post by:
I know all the hype right now from Microsoft is how much easier, faster, and less code ASP.NET 2.0 provides over previous versions. I'm puzzled by this as I could turn out an classic ASP webpage...
18
by: John Henry | last post by:
If I have a list of say, 10 elements and I need to slice it into irregular size list, I would have to create a bunch of temporary variables and then regroup them afterwords, like: # Just for...
1
by: Matt Chisholm | last post by:
Hi. I was wondering if there had ever been an official decision on the idea of adding labeled break and continue functionality to Python. I've found a few places where the idea has come up, in...
12
by: danmcleran | last post by:
All, I can't seem to find an answer to this question anywhere, but I'm still looking. My problem is I have a list of values like this: l = A value with bit 0x80 set delineates the start of...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.