10

Possible Duplicates:
splitting a list of arbitrary size into only roughly N-equal parts
How do you split a list into evenly sized chunks in Python?

I need to create a function that will split a list into a list of list, each containing an equal number of items (or as equal as possible).

e.g.

def split_lists(mainlist, splitcount):
    ....


mylist = [1,2,3,4,5,6]

split_list(mylist,2) will return a list of two lists of three elements - [[1,2,3][4,5,6]].

split_list(mylist,3) will return a list of three lists of two elements.

split_list(mylist,4) will return a list of two lists of two elements and two lists of one element.

I don't care which elements appear in which list, just that the list is divided up as evenly as possible.

Community
  • 1
  • 1
Richard
  • 743
  • 2
  • 6
  • 12
  • 2
    I thought this looked familiar: http://stackoverflow.com/questions/2130016/splitting-a-list-of-arbitrary-size-into-only-roughly-n-equal-parts – Ignacio Vazquez-Abrams Feb 10 '10 at 09:03
  • This question and its companion "clever answer" using zip and iter() has come up about 5 times in the past 48 hours. Search is your friend. – drxzcl Feb 10 '10 at 09:04
  • search "python list chunks" and you'll find your answer... voting to close as duplicate – fortran Feb 10 '10 at 09:09
  • 3
    This question asks for splitting sequence into *specified number* of chunks, not the chunks of *specified length*. This is not the same thing. – zgoda Oct 31 '11 at 10:09

1 Answers1

8

numpy.split does this already:

Examples:

>>> mylist = np.array([1,2,3,4,5,6])

split_list(mylist,2) will return a list of two lists of three elements - [[1,2,3][4,5,6]].

>>> np.split(mylist, 2)
[array([1, 2, 3]), array([4, 5, 6])]

split_list(mylist,3) will return a list of three lists of two elements.

>>> np.split(mylist, 3)
[array([1, 2]), array([3, 4]), array([5, 6])]

split_list(mylist,4) will return a list of two lists of two elements and two lists of one element.

You may probably want to add an exception capture for the cases when the remainder of length(mylist)/n is not 0:

>>> np.split(mylist, 4)
ValueErrorTraceback (most recent call last)
----> 1 np.split(mylist, 4)
...
ValueError: array split does not result in an equal division
dalloliogm
  • 8,718
  • 6
  • 45
  • 55
  • 3
    Really bad, doesn't answer the question, nor does it work. – hisairnessag3 Jun 05 '18 at 07:25
  • I've added some examples to make the answer more clear. To be fair, the original questions is not very clear, but I think numpy.split does what the OP wants. At least the examples for n=2 and n=3 seem to return the same outputs as in the question (except that the output is a numpy array instead of a list, but I've assumed that this is not a problem) – dalloliogm Jun 05 '18 at 09:57
  • Question asks "how to split a list" not a np array. But I reversed my downvote to up nevertheless. – hisairnessag3 Jun 05 '18 at 13:59
  • 4
    For ex this works: `def split_list(alist, wanted_parts=1): length = len(alist) return [ alist[i*length // wanted_parts: (i+1)*length // wanted_parts] for i in range(wanted_parts) ]` – hisairnessag3 Jun 05 '18 at 14:00
  • 4
    `np.array_split` has the same syntax and avoids the possibility of a `ValueError` – Addison Klinke May 20 '21 at 20:51