You can write it fairly simply as a list generator:
def split(a, n):
k, m = divmod(len(a), n)
return (a[i*k+min(i, m):(i+1)*k+min(i+1, m)] for i in range(n))
Example:
>>> list(split(range(11), 3))
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10]]
More Related Contents:
- How do you split a list into evenly sized chunks?
- What is the most “pythonic” way to iterate over a list in chunks?
- How do I split a list into equally-sized chunks?
- How to iterate over a list in chunks
- How do you split reading a large csv file into evenly-sized chunks in Python?
- Compare two lists value-wise in Python [closed]
- How do you find min and max without using min and max function [duplicate]
- how do I make two dictionary keys with value as list [closed]
- Access a component of a list
- How to split a string into a list?
- Matrix Transpose in Python [duplicate]
- How do I find the duplicates in a list and create another list with them?
- Print list without brackets in a single row
- Removing elements that have consecutive duplicates
- Is there a simple way to delete a list element by value?
- Different behaviour for list.__iadd__ and list.__add__
- Slicing a list in Python without generating a copy
- How do I check if there are duplicates in a flat list?
- Memory errors and list limits?
- [] and {} vs list() and dict(), which is better?
- How to find the maximum value in a list of tuples? [duplicate]
- How to access List elements
- In Python, why is list[] automatically global?
- Understand Python swapping: why is a, b = b, a not always equivalent to b, a = a, b?
- convert a string which is a list into a proper list python
- Get difference between two lists with Unique Entries
- Empty list boolean value
- Counting the number of True Booleans in a Python List
- Combining lists into one [duplicate]
- split string in python to get one value?