I want to split my list into lines with the same number of columns, I'm looking for the best (most elegant / pythonic) way to achieve this:
>>> split.split_size([1,2,3], 5, 0) [[1, 2, 3, 0, 0]] >>> split.split_size([1,2,3,4,5], 5, 0) [[1, 2, 3, 4, 5]] >>> split.split_size([1,2,3,4,5,6], 5, 0) [[1, 2, 3, 4, 5], [6, 0, 0, 0, 0]] >>> split.split_size([1,2,3,4,5,6,7], 5, 0) [[1, 2, 3, 4, 5], [6, 7, 0, 0, 0]]
What I came up with so far:
def split_size(l, size, fillup): """ splits list into chunks of defined size, fills up last chunk with fillup if below size """
I am sure that there should be some more reasonable solutions. Especially for the “reverse mod” part: len (l)% size or size should be a more readable way to do this, no?