Skip to content

Instantly share code, notes, and snippets.

@marcellmars
Created May 21, 2014 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcellmars/a5dad641aa69b4aed111 to your computer and use it in GitHub Desktop.
Save marcellmars/a5dad641aa69b4aed111 to your computer and use it in GitHub Desktop.
list2chunks returns new list with chunks from the input list. chunks are having lenghts of chlen and with the last chunk being merged if shorter than chlen
def list2chunks(lst, chlen=10, chcount=0, chunks=[], tail = 0.5):
'''it returns new list with chunks from the input list. \
chunks are having lenghts of chlen and with the last chunk \
being merged if shorter than chlen'''
if len(lst) <= chlen + tail * chlen:
chunks.append(lst[chcount:])
return chunks
chunk = lst[chcount:chcount+chlen]
chcount += chlen
chunks.append(chunk)
return list2chunks(lst[chcount:], chunks=chunks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment