Given a slice object
slice(1, 100, None)
How can I get a a tuple / list (1,100,None)
without parsing the string representation of the slice object. With the string representation, it's fairly obvious but is there a way to get these values from the object itself?
To extract elements with specific indices from a Python list, use slicing list[start:stop:step] . If you cannot use slicing because there's no pattern in the indices you want to access, use the list comprehension statement [lst[i] for i in indices] , assuming your indices are stored in the variable indices .
Consider a python list, In-order to access a range of elements in a list, you need to slice a list. One way to do this is to use the simple slicing operator i.e. colon(:) With this operator, one can specify where to start the slicing, where to end, and specify the step.
The items at interval 2 starting from the last index are sliced. If you want the items from one position to another, you can mention them from start to stop . The items from index 1 to 4 are sliced with intervals of 2.
Python slice() Function The slice() function returns a slice object. A slice object is used to specify how to slice a sequence. You can specify where to start the slicing, and where to end. You can also specify the step, which allows you to e.g. slice only every other item.
def slice_tuple(slice_):
return slice_.start, slice_.stop, slice_.step
I'm pretty sure this has been asked before, but Google didn't turn up the duplicates.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With