Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41987 > unrolled thread

Re: Splitting a list into even size chunks in python?

Started byPeter Otten <__peter__@web.de>
First post2013-03-27 09:27 +0100
Last post2013-03-28 11:15 +1100
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Splitting a list into even size chunks in python? Peter Otten <__peter__@web.de> - 2013-03-27 09:27 +0100
    Re: Splitting a list into even size chunks in python? Roy Smith <roy@panix.com> - 2013-03-27 08:51 -0400
      Re: Splitting a list into even size chunks in python? Chris Angelico <rosuav@gmail.com> - 2013-03-27 23:59 +1100
        Re: Splitting a list into even size chunks in python? Roy Smith <roy@panix.com> - 2013-03-27 19:42 -0400
          Re: Splitting a list into even size chunks in python? Chris Angelico <rosuav@gmail.com> - 2013-03-28 11:15 +1100

#41987 — Re: Splitting a list into even size chunks in python?

FromPeter Otten <__peter__@web.de>
Date2013-03-27 09:27 +0100
SubjectRe: Splitting a list into even size chunks in python?
Message-ID<mailman.3796.1364372856.2939.python-list@python.org>
Norah Jones wrote:

> I have a list of arbitrary length, and I need to split it up into equal
> size chunks. There are some obvious ways to do this, like keeping a
> counter and two lists, and when the second list fills up, add it to the
> first list and empty the second list for the next round of data, but this
> is potentially extremely expensive.
> 
> I was wondering if anyone had a good solution to this for lists of any
> length
> 
> This should work:
> 
>     l = range(1, 1000)
>     print chunks(l, 10) -> [ [ 1..10 ], [ 11..20 ], .., [ 991..999 ] ]
> 
> I was looking for something useful in itertools but I couldn't find
> anything obviously useful.

Look again, for the grouper() recipe. For lists you can also use slicing:

>>> items
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> n = 3
>>> [items[start:start+n] for start in range(0, len(items), n)]
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g']]

[toc] | [next] | [standalone]


#42011

FromRoy Smith <roy@panix.com>
Date2013-03-27 08:51 -0400
Message-ID<roy-6AE143.08513627032013@70-1-84-166.pools.spcsdns.net>
In reply to#41987
In article <mailman.3796.1364372856.2939.python-list@python.org>,
 Peter Otten <__peter__@web.de> wrote:

> Look again, for the grouper() recipe.

Grouper() is good tty.cooked() with just a little time.time() and 
crypt.salt()

[toc] | [prev] | [next] | [standalone]


#42013

FromChris Angelico <rosuav@gmail.com>
Date2013-03-27 23:59 +1100
Message-ID<mailman.3809.1364389191.2939.python-list@python.org>
In reply to#42011
On Wed, Mar 27, 2013 at 11:51 PM, Roy Smith <roy@panix.com> wrote:
> In article <mailman.3796.1364372856.2939.python-list@python.org>,
>  Peter Otten <__peter__@web.de> wrote:
>
>> Look again, for the grouper() recipe.
>
> Grouper() is good tty.cooked() with just a little time.time() and
> crypt.salt()

Huh, an inversion of http://xkcd.com/282/

ChrisA

[toc] | [prev] | [next] | [standalone]


#42049

FromRoy Smith <roy@panix.com>
Date2013-03-27 19:42 -0400
Message-ID<roy-4B6711.19423627032013@70-1-84-166.pools.spcsdns.net>
In reply to#42013
In article <mailman.3809.1364389191.2939.python-list@python.org>,
 Chris Angelico <rosuav@gmail.com> wrote:

> On Wed, Mar 27, 2013 at 11:51 PM, Roy Smith <roy@panix.com> wrote:
> > In article <mailman.3796.1364372856.2939.python-list@python.org>,
> >  Peter Otten <__peter__@web.de> wrote:
> >
> >> Look again, for the grouper() recipe.
> >
> > Grouper() is good tty.cooked() with just a little time.time() and
> > crypt.salt()
> 
> Huh, an inversion of http://xkcd.com/282/
> 
> ChrisA

Thyme is an herb, not a spice.  Randall should know better.

[toc] | [prev] | [next] | [standalone]


#42050

FromChris Angelico <rosuav@gmail.com>
Date2013-03-28 11:15 +1100
Message-ID<mailman.3832.1364429710.2939.python-list@python.org>
In reply to#42049
On Thu, Mar 28, 2013 at 10:42 AM, Roy Smith <roy@panix.com> wrote:
> In article <mailman.3809.1364389191.2939.python-list@python.org>,
>  Chris Angelico <rosuav@gmail.com> wrote:
>
>> On Wed, Mar 27, 2013 at 11:51 PM, Roy Smith <roy@panix.com> wrote:
>> > In article <mailman.3796.1364372856.2939.python-list@python.org>,
>> >  Peter Otten <__peter__@web.de> wrote:
>> >
>> >> Look again, for the grouper() recipe.
>> >
>> > Grouper() is good tty.cooked() with just a little time.time() and
>> > crypt.salt()
>>
>> Huh, an inversion of http://xkcd.com/282/
>>
>> ChrisA
>
> Thyme is an herb, not a spice.  Randall should know better.

And in case this wasn't off topic enough already... That statement
shows that you are, almost certainly, American - out here, I would say
instead that thyme is a herb.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web