22

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?

2
  • The Python Slice Notation Commented Jul 12, 2013 at 7:55
  • This could also be interesting for you: print(list(reversed(range(5)))) Commented Jul 12, 2013 at 7:56

6 Answers 6

23

You can assign your variable to None:

>>> a = range(20)
>>> a[15:None:-1]
[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> 
Sign up to request clarification or add additional context in comments.

2 Comments

This (using None) is probably the best solution, but note that you can also use negative indices all the way around: a[-5:-21:-1] gets you the same slice, including the final a[0] element.
@torek I also thought of using negative indices, but I think that is the best solution, because you can avoid the extra assignment to None (see my answer).
15

Omit the end index:

print a[15::-1]

6 Comments

start and end are actually variables.
do I need a special if statement for this?
you can do it without an if statement if you don't mind doing this in two steps: print a[low:high+1][::-1]
@MohammadMoghimi You can use None.
My list is very long. I think this slices the list first and then reverses it.
|
8

In Python2.x, the simplest solution in terms of number of characters should probably be :

>>> a=range(20)

>>> a[::-1]
[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

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.

>>> a=xrange(20)
>>> a[::-1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence index must be integer, not 'slice'

After in Python3.x, range() does what xrange() does in Python2.x but also has an improvement accepting indexing change upon the object.

>>> a = range(20)
>>> a[::-1]
range(19, -1, -1)
>>> b=a[::-1]
>>> for i in b:
...     print (i)
... 
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
>>> 

the difference between range() and xrange() learned from source: http://pythoncentral.io/how-to-use-pythons-xrange-and-range/ by author: Joey Payne

2 Comments

Wut? Is this an answer?
My direct answer is just a[::-1]. The following part is some possible complications people may find if they are using different version of python. Sorry about the confusion. I am new here, do you think i should delete the non-direct answer part and where should i put it? Thanks!
1

If you use negative indexes you can avoid extra assignments, using only your start and end variables:

a = range(20)
start = 20
for end in range(21):
    a[start:-(len(a)+1-end):-1]

Comments

1
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> print a[:6:-1]
[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7]
>>> a[7] == a[:6:-1][-1]
True
>>> a[1] == a[:0:-1][-1]
True

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:

>>> a[0:2:]
[0, 1]

-1 is the last value in a:

>>> a[len(a)-1] == a[-1]
True

Comments

0

EDIT: begin and end are variables

I never realized this, but a (slightly hacky) solution would be:

>>> a = range(5)
>>> s = 0
>>> e = 3
>>> b = a[s:e]
>>> b.reverse()
>>> print b
[2, 1, 0]

1 Comment

start and end are variables

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.