Python slice() function returns a slice object representing the set of indices specified by range(start, stop, step).
Python slice()
Python slice function syntax is:
1 2 3 4 |
class slice(stop) class slice(start, stop[, step]) |
Note that slice function returns slice object. We can pass this object as a set of indices with sequences such as string, list, tuple etc.
Python slice function allows us to create a stepwise sub-sequence easily without doing complete iteration on the existing sequence.
slice() function arguments
- start: specifies the start of the index value. It’s optional and defaults to None.
- stop: specifies the end of the index value. This is a mandatory parameter.
- step: specifies the steps to take from start to stop index. It’s an optional parameter and defaults to None.
Python slice object has read-only data attributes – start, stop and step – which return the argument values (or default value).
Let’s see how to create slice objects.
1 2 3 4 5 6 7 8 9 10 11 |
s = slice(1, 10, 2) # indexes 1,3,5,7,9 print(type(s)) print(s.start) print(s.stop) print(s.step) s = slice(5) # indexes 0,1,2,3,4 print(s.start) print(s.stop) print(s.step) |
Output:
1 2 3 4 5 6 7 8 9 |
1 10 2 None 5 None |
Python slice object has no use on its own, it’s useful when used in conjunction with sequences to create a sub-sequence.
Python slice string
Let’s see how to use slice function with string. We will pass slice object just like a normal index to get the substring value from a string.
1 2 3 4 |
s = slice(1, 10, 2) # indexes 1,3,5,7,9 print('abcde'[s]) |
Output: bd
Note that if the slice indexes are more than the length of the sequence, no exception is raised and data is returned till the maximum available index.
We can also pass negative values for slice() function. In that case, the iteration will be performed backward i.e. from end to start.
1 2 3 4 |
s = slice(-1, -3, -1) # indexes -1, -2 print('abcde'[s]) |
Output: ed
Python slice list/array
Let’s look at an example of using slice() function with list or array.
1 2 3 4 5 |
s = slice(0, 3) # indexes 0, 1, 2 my_list = [1, 2, 3, 4, 5, 6] print(my_list[s]) |
Output: [1, 2, 3]
Python slice tuple
We can use slicing with tuple too because it’s a sequence.
1 2 3 4 5 |
s = slice(2) my_tuple = [1, 2, 3, 4, 5, 6] print(my_tuple[s]) |
Output: [1, 2]
Python slice extended indexing syntax
Since slicing is very popular in numerical python, there is a shorthand way to create a slice object.
1 2 3 |
a[start:stop:step] |
Let’s see some examples of slicing using the shorthand approach.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
x = 'abcde' y = x[1:3:1] # 1,2 print(y) y = x[1:3] # 1,2 print(y) y = x[2:] # 2 to length of sequence print(y) y = x[:5:2] # 0,2,4 print(y) y = x[:] # copy of sequence print(y) y = x[-1:-4:-1] # reverse iteration, end to start print(y) |
Output:
1 2 3 4 5 6 7 8 |
bc bc cde ace abcde edc |
The output is self-explanatory and important details are already mentioned in the comments.
Summary
Python slice() is a very useful function. We can create sub-sequence based on steps, start and end indexes easily without doing complete iteration.
Reference: Official Documentation