Skip to content

Instantly share code, notes, and snippets.

@particledecay
Last active July 24, 2017 13:21
Show Gist options
  • Select an option

  • Save particledecay/d508a567b7334218b8b7ec1dd8b241c7 to your computer and use it in GitHub Desktop.

Select an option

Save particledecay/d508a567b7334218b8b7ec1dd8b241c7 to your computer and use it in GitHub Desktop.
def chop(sliceable, columns):
"""Return multiple selected slices of an iterable, in desired order.
Args:
sliceable (list): the original list to chop up
columns (list): a list of integers (indices) or tuples (slices)
Returns:
(list) the final chopped up list
Examples:
>>> test_row = ['zero', 'one', 'two', 'three', 'four', 'five']
>>> chop(test_row, [0, 2, 4])
['zero', 'two', 'four']
>>> chop(test_row, [(1, 3), 5])
['one', 'two', 'five']
"""
slices = map(lambda x: slice(*x) if isinstance(x, tuple) else slice(x, x + 1), columns)
chopped = [sliceable[c] for c in slices]
return list(itertools.chain.from_iterable(chopped))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment