Divide a list into groups - how?

I’ve thrown together some code to build an HTML list of links to images in a folder but I’d like to modify it to create several HTML lists, each with only 20 items. Here’s what I have for getting the file names and building a list of HTML list items:

set theFolder to choose folder"

tell application "Finder"
	set repeat_list to the name of every file of theFolder
	set new_List to {}
	repeat with an_item in repeat_list
		set end of new_List to "<li><a href=\"images/full/" & an_item & "\"><img src=\"images/thumb/" & an_item & "\" alt=\"" & an_item & "\" /></a></li>" & return
	end repeat
end tell

So, I think what I’d like to be able to do is somehow divide the list into smaller lists of 20 items each (assuming there are more than 20 items) as many times as necessary so I end up with a series of lists of 20 items each.

Suggestions anyone? Thanks.

Something like this?


set list_with_lists to {}
set new_list to {}
set counter to 1
set page_size to 20

repeat with an_item in repeat_list
	set end of new_list to "<li><a href=\"images/full/" & an_item & "\"><img src=\"images/thumb/" & an_item & "\" alt=\"" & an_item & "\" /></a></li>" & return
	
	if counter = page_size then
		set end of listWithLists to new_list
		set new_list to {}
		set counter to 1
	else
		set counter to counter + 1
	end if
end repeat

Yes! I think that does it - thank you DJ Bazzie Wazzie! So this leaves me with a list in which each list item is a list of up to 20 items? I knew it had to be something along those lines but couldn’t manage to get all the pieces in place.

Thanks again.

Yes and the last item contains 1 - 20 items. You’re welcome!