I'm having some trouble figuring out how to slice python lists, it is illustrated as follows:
>>> test = range(10)
>>> test
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> test[3:-1]
[3, 4, 5, 6, 7, 8]
>>> test[3:0]
[]
>>> test[3:1]
[]
>>> test
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
To my understanding, python slice means lst[start:end], and including start, excluding end. So how would i go about finding the "rest" of a list starting from an element n?
Thanks a lot for all your help!
You can leave one end of the slice open by not specifying the value.
You can also use the
None
keyword for the end parameter when slicing. This would also return the elements till the end of the list (or any sequence such as tuple, string, etc.)Simply omit the end.
Leaving out the end still works when you want to skip some:
Return a slice of the list after a starting value:
If you're using a variable as the range endpoint, you can use
None
.