I want to pass a slice to a function so I can select a portion of a list. Passing as a string is preferable as I will read the required slice as a command line option from the user.
def select_portion(list_to_slice, slicer):
return(list_to_slice[slicer])
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
slicer = "1:4"
print(select_portion(numbers, slicer))
I get the following:
TypeError: list indices must be integers or slices, not str
which is understandable but I don't know how to modify this to get the intended output of:
[1, 2, 3]