1

This is probably silly and basic but how do I get the list edcba

When I try:

import string
letters = string.ascii_lowercase
letters[4:0:-1]

'edcb'

This somehow makes sense since the index is [4,3,2,1]. However, using -1 results in the following:

letters[4:-1:-1]

''

I know I could just do "".join(reversed(letters[:5])) or use list comprehension but I am curious on how to do it with negative steps.

Jrakru56
  • 1,211
  • 9
  • 16
  • Sounds like an X-Y problem to be honest... – meowgoesthedog Dec 03 '18 at 18:24
  • 2
    Possible duplicate of [How can I reverse a list in Python?](https://stackoverflow.com/questions/3940128/how-can-i-reverse-a-list-in-python) – kofemann Dec 03 '18 at 18:25
  • Sorry! what's an X-Y problem? and I was the reason it is not a duplicate is because the other question is only about reversing the list, I am trying to reverse and slice at the same time. I was using two concepts that individually work but together gave me a somewhat unexpected result. – Jrakru56 Dec 03 '18 at 18:56
  • You might wish to [read this for more info on the "X-Y Problem"](http://xyproblem.info/). Here's the [related FAQ directly from meta SE](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – r.ook Dec 03 '18 at 19:54

3 Answers3

5

letters[4::-1], which is basically letters[4:None:-1]

Think of the slice this way:

[element to start at: 
 element to start excluding: 
 steps]

So by not explicitly stating 0 in the second part of the slice, you're telling the interpreter to start from index 4 and go step -1 until no elements are found.

Another way to visualize this is if you were to do this forward and want "vwxyz", you wouldn't use letters[21:25:1], but just letters[21:] which translates to letters[21:None:1]

r.ook
  • 13,466
  • 2
  • 22
  • 39
0
import string
letters = string.ascii_lowercase
lent = len(letters)
data = []
for l in range(1,lent):
    total = lent - int(l)
    data.append(letters[total])
data.append(letters[0])
print(data)
Md Jewele Islam
  • 939
  • 8
  • 18
  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find [how to write a good answer](https://stackoverflow.com/help/how-to-answer) very helpful. Please edit your answer - [From Review](https://stackoverflow.com/review/low-quality-posts/21586061) – Nick Dec 04 '18 at 05:51
  • Thank you for your suggestion – Md Jewele Islam Dec 04 '18 at 08:42
0
letters[4:-1:-1]

The reason why you unexpectedly get an empty string as the result is that you've asked to go from index 4, until reaching the end of the string (-1), by steps of -1.

The index -1 in this context is equivalent to the index 25, so you've essentially done letters[4:25:-1] -- as soon as python tries to evaluate the first item in the slice it's done because you've already stepped beyond the bounds. The same sort of expression with a positive increment would be letters[5:4] -- it's done before it starts.

bstpierre
  • 30,042
  • 15
  • 70
  • 103