Skip to content

Instantly share code, notes, and snippets.

@hctilg
Created October 11, 2024 18:37
Show Gist options
  • Save hctilg/050f4636748eb3424c17de93daa5f0ea to your computer and use it in GitHub Desktop.
Save hctilg/050f4636748eb3424c17de93daa5f0ea to your computer and use it in GitHub Desktop.
"The number of segments cannot exceed the length of the list!"
def split_list(lst, n):
if n <= 0: return []
elif n > len(lst):
return "The number of segments cannot be greater than the length of the list!"
else:
avg = len(lst) / n
out = []
last = 0.0
while last < len(lst):
out.append(lst[int(last):int(last + avg)])
last += avg
return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment