Skip to content

Instantly share code, notes, and snippets.

@CodeMan99
Created May 7, 2021 15:00
Show Gist options
  • Save CodeMan99/c9ca87dbec613b19f5cc730791eca754 to your computer and use it in GitHub Desktop.
Save CodeMan99/c9ca87dbec613b19f5cc730791eca754 to your computer and use it in GitHub Desktop.
Create left & right slices of a collection
from operator import itemgetter
def create_slicer(*indexes):
"""
>>> l = list(range(10))
>>> create_slicer(5)(l)
([0, 1, 2, 3, 4], [5, 6, 7, 8, 9])
>>> create_slicer(3, 7)(l)
([0, 1, 2], [3, 4, 5, 6], [7, 8, 9])
>>> create_slicer(3, 5, 7)(l)
([0, 1, 2], [3, 4], [5, 6], [7, 8, 9])
"""
left = (None,) + indexes
right = indexes + (None,)
return itemgetter(*(slice(l, r) for l, r in zip(left, right)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment