Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get index values from slice objects in python [duplicate]

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?

like image 420
Sid Shukla Avatar asked May 04 '16 00:05

Sid Shukla


People also ask

How do you slice a specific index in Python?

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 .

What is :: In slicing?

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.

When slicing in Python What does the 2 in 2 specify?

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.

What does slice () do in Python?

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.


1 Answers

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.

like image 71
user2357112 supports Monica Avatar answered Nov 15 '22 07:11

user2357112 supports Monica