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

Discussion in 'Python' started by Peter Otten, Mar 27, 2013.

  1. Peter Otten

    Peter Otten Guest

    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']]
     
    Peter Otten, Mar 27, 2013
    #1
    1. Advertising

  2. Peter Otten

    Roy Smith Guest

    In article <>,
    Peter Otten <> wrote:

    > Look again, for the grouper() recipe.


    Grouper() is good tty.cooked() with just a little time.time() and
    crypt.salt()
     
    Roy Smith, Mar 27, 2013
    #2
    1. Advertising

  3. On Wed, Mar 27, 2013 at 11:51 PM, Roy Smith <> wrote:
    > In article <>,
    > Peter Otten <> 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
     
    Chris Angelico, Mar 27, 2013
    #3
  4. Peter Otten

    Roy Smith Guest

    In article <>,
    Chris Angelico <> wrote:

    > On Wed, Mar 27, 2013 at 11:51 PM, Roy Smith <> wrote:
    > > In article <>,
    > > Peter Otten <> 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.
     
    Roy Smith, Mar 27, 2013
    #4
  5. On Thu, Mar 28, 2013 at 10:42 AM, Roy Smith <> wrote:
    > In article <>,
    > Chris Angelico <> wrote:
    >
    >> On Wed, Mar 27, 2013 at 11:51 PM, Roy Smith <> wrote:
    >> > In article <>,
    >> > Peter Otten <> 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
     
    Chris Angelico, Mar 28, 2013
    #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. John Ericson
    Replies:
    0
    Views:
    431
    John Ericson
    Jul 19, 2003
  2. Drew Olson
    Replies:
    33
    Views:
    981
    Drew Olson
    Dec 2, 2006
  3. Tore Aursand

    Splitting an array into "even" parts

    Tore Aursand, Nov 5, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    194
    Tore Aursand
    Nov 5, 2003
  4. Norah Jones
    Replies:
    0
    Views:
    123
    Norah Jones
    Mar 27, 2013
  5. Arnaud Delobelle
    Replies:
    0
    Views:
    160
    Arnaud Delobelle
    Mar 27, 2013
Loading...

Share This Page