Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: Splitting a list into even size chunks in python? (http://www.velocityreviews.com/forums/t959125-re-splitting-a-list-into-even-size-chunks-in-python.html)

 Peter Otten 03-27-2013 08:27 AM

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

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']]

 Roy Smith 03-27-2013 12:51 PM

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

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()

 Chris Angelico 03-27-2013 12:59 PM

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

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

 Roy Smith 03-27-2013 11:42 PM

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

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.

 Chris Angelico 03-28-2013 12:15 AM

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

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

 All times are GMT. The time now is 01:01 AM.