Taking a list and using it to build columns Say I have [1, 2, 3, 4, 5, 6, 7, 8, 9] and I want to spread it across - at most - 4 columns. The end result would be: [[1, 2, 3], [4, 5], [6, 7], [8, 9]]. So I made this:

item_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
item_count = len(item_list)
column_count = item_count if item_count &lt;= 4 else 4
items_per_column = int(item_count / 4)
extra_items = item_count % 4

item_columns = []

for group in range(column_count):
    count = items_per_column
    if group &lt; extra_items:
        count += 1

    item_columns.append(item_list[:count])
    item_list = item_list[count:]


Is there a nicer way of accomplishing this?
You could do something like this (elegant, but not the fastest approach)

item_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
min_height = len(item_list) // 4
mod = len(item_list) % 4

heights = [min_height + 1] * mod + [min_height] * (4 - mod)
indices = [sum(heights[:i]) for i in range(4)]
columns = [item_list[i:i+h] for i, h in zip(indices, heights)]


Or a compressed version:

columns = [item_list[
    min_height * i + (i &gt;= mod) * mod :
    min_height * (i+1) + (i+1 &gt;= mod) * mod]
    for i in range(4)]


I guess the fastest way would be similar to what you already have:

index = 0
columns = []
for i in xrange(4):
    height = min_height + (i &lt; mod)
    columns.append(item_list[index:index+height])
    index += height


(With both of these, if there are less than 4 items you get empty columns at the end.)