16
loading...
This website collects cookies to deliver better user experience
Read the original post on my blog
start
index and an end
index. Not providing either the start or the end will result in you getting all the numbers from the beginning and/or the end.>>> nums = [1, 2, 3, 4, 5, 6]
>>> nums[1:3]
[2, 3]
>>> nums[:3]
[1, 2, 3]
>>> nums[3:]
[4, 5, 6]
Note that the end
index, when provided, is never included in the result.
>>> nums[:]
[1, 2, 3, 4, 5, 6]
step
, which tells how many numbers it should increment its index by, to get to the next element. step
is 1 by default.>>> nums = [1, 2, 3, 4, 5, 6]
>>> nums[::1]
[1, 2, 3, 4, 5, 6]
>>> nums[::2]
[1, 3, 5]
>>> nums[::4]
[1, 5]
def slice(array, start, stop, step=1):
result = []
index = start
while index < stop:
result.append(array[index])
index += step
return result
end
never being included, and how step
decides how to pick the next value.>>> nums[:-1]
[1, 2, 3, 4, 5]
>>> nums[:-3]
[1, 2, 3]
>>> nums[-3:-1]
[4, 5]
>>> nums[-1:-3:-1]
[6, 5]
>>> nums[-1:-3]
[]
>>> nums = [1, 2, 3, 4, 5, 6]
>>> nums[-1] # last index
6
>>> nums[-2] # second from the end
5
>>> nums[len(nums)-2] # it's the same thing
5
start
or stop
value, it will be treated as that same index from the end.>>> nums[ 3 : 5]
[4, 5]
>>> nums[6-3 : 6-1]
[4, 5]
>>> nums[ -3 : -1]
[4, 5]
>>> nums[:-1] # all values except the last one
[1, 2, 3, 4, 5]
>>> nums[:-3] # all values except the last three
[1, 2, 3]
>>> nums[-3:] # all values from last 3rd
[4, 5]
slice
Python function that factors this in:def slice(array, start, stop, step=1):
if start < 0:
start = len(array) + start
if stop < 0:
stop = len(array) + stop
result = []
index = start
while index < stop:
result.append(array[index])
index += step
return result
step
value. I'm sure you must have seen this one rather un-intuitive way to reverse a list in Python:>>> nums[::-1]
[6, 5, 4, 3, 2, 1]
>>> nums[2::-1] # will get indices 2, 1 and 0
[3, 2, 1]
step
is negative, the condition that's used to determine whether to take the next element or not is flipped around.while start < end
while also decrementing start
at every step, we will never reach the point where the condition becomes false. So we need to flip the condition around to while start > end
, in order for slicing to still work.nums[-1:-3:-1]
returns [6, 5]
, it's because it starts with the last index, and keeps going until it's decremented till the 3rd last index (which is excluded).def slice(array, start, stop, step=1):
if start < 0:
start = len(array) + start
if stop < 0:
stop = len(array) + stop
result = []
index = start
if step >= 0:
while index < stpp:
result.append(array[index])
index += step
else:
# Negative slice
while index > stop:
result.append(array[index])
index += step
return result
But what about nums[-1:-3]
returning an empty list?
start < end
is False
from the get go, and the result just stays empty.slice
function isn't an exact implementation of the algorithm, though it comes close. Currently it has no way of not specifying a start or an end, and it also creates an infinite loop for step=0
. But apart from that, it's pretty much identical to the Python builtin slice implementation.