Splitting a list into even size chunks in python?

Discussion in 'Python' started by Norah Jones, Mar 27, 2013.

  1. Norah Jones

    Norah Jones Guest

    Hi,

    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.

    Appretiate your help.
    Norah Jones, Mar 27, 2013
    #1
    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:
    416
    John Ericson
    Jul 19, 2003
  2. Drew Olson
    Replies:
    33
    Views:
    912
    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:
    184
    Tore Aursand
    Nov 5, 2003
  4. Peter Otten
    Replies:
    4
    Views:
    160
    Chris Angelico
    Mar 28, 2013
  5. Arnaud Delobelle
    Replies:
    0
    Views:
    145
    Arnaud Delobelle
    Mar 27, 2013
Loading...

Share This Page