Skip to content

Instantly share code, notes, and snippets.

@terngkub
Created November 16, 2020 11:58
Show Gist options
  • Select an option

  • Save terngkub/c7c60a57c30a748b218186be85bc2aea to your computer and use it in GitHub Desktop.

Select an option

Save terngkub/c7c60a57c30a748b218186be85bc2aea to your computer and use it in GitHub Desktop.
Python: split a list into a list of chunks
def split_list(lst, chunk_nb):
list_size = len(lst)
ret = []
for i in range(chunk_nb):
beg = round(list_size * i / chunk_nb)
end = round(list_size * (i+1) / chunk_nb)
ret.append(lst[beg:end])
return ret
splits = split_list(list(range(10)), 3)
for split in splits:
print(split)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment