I know that a[end:start:-1] slices a list in a reverse order.
For example
a = range(20)
print a[15:10:-1] # prints [15, ..., 11]
print a[15:0:-1] # prints [15, ..., 1]
but you cannot get to the first element (0 in the example). It seems that -1 is a special value.
print a[15:-1:-1] # prints []
Any ideas?
In Python2.x, the simplest solution in terms of number of characters should probably be :
Though i want to point out that if using xrange(), indexing won't work because xrange() gives you an xrange object instead of a list.
After in Python3.x, range() does what xrange() does in Python2.x but also has an improvement accepting indexing change upon the object.
the difference between range() and xrange() learned from source: http://pythoncentral.io/how-to-use-pythons-xrange-and-range/ by author: Joey Payne
You can assign your variable to
None
:Omit the end index:
So as you can see when subsitute a value in start label :end: it will give you from start to end exclusively a[end].
As you can see in here as well:
-1 is the last value in a:
If you use negative indexes you can avoid extra assignments, using only your start and end variables:
EDIT: begin and end are variables
I never realized this, but a (slightly hacky) solution would be: