Skip to content

Instantly share code, notes, and snippets.

@JacobCallahan
Last active August 14, 2020 04:30
Show Gist options
  • Save JacobCallahan/b34fc193dbcc1eb769961c11224c5888 to your computer and use it in GitHub Desktop.
Save JacobCallahan/b34fc193dbcc1eb769961c11224c5888 to your computer and use it in GitHub Desktop.
A list that you can divide... why not?
from collections import UserList
class DivList(UserList):
def __truediv__(self, num):
list_len = len(self.data)
start = 0
step, rem = divmod(list_len, num)
stop = start + step + bool(rem)
outlist = []
while start < list_len:
if rem:
rem -= 1
outlist.append(self.data[start:stop])
start, stop = stop, stop + step + bool(rem)
return outlist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment