Thursday, July 29, 2010

Re: Best way to present N items in table row for list

Hi Wadim,

On Jul 27, 2010, at 6:51 PM, Wadim wrote:
> Hello.
> I have a list that i want to present in a table.
> Each row should have 5 items.
> For now i create the function that returns me new grouped list, that i
> use later in a template.
> def groupListByRow(list):
> cnt=0
> rows=[]
> while cnt<len(list):
> row=[]
> for x in range(5):
> if cnt<len(list):
> row.append(list[cnt])
> else:
> row.append(None)
> cnt+=1
> rows.append(row)
> return rows;
> is there better way to do this? May be it is possible to do it inside
> template?

First: list is a very unlucky name for a parameter, since it's the
type of []

Second: list comprehension rules!

def groupListByRow(list):
return [lst[start:start+5] for start in range(0, len(lst), 5)]

Cheers, Roald

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment