1

Below is the code. I don't understand the meaning of the last bracket in [::-1][:-1], and how come can you write two brackets at the same time. I understand that the first slice bracket reverses the order of the string, but what does the second do?

for i in range(n,0,-1):
    temp = list(alphabets[:n][i-1:])
    print('-'.join(temp[::-1][:-1]+temp).center(4*n-3,'-'))

Thanks for cooperation!

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Nikita
  • 35
  • 4
  • 2
    Does this answer your question? [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation). You can chain slices as much as you want--they are processed left to right with results being passed from each slice to the next. – ggorlen Dec 27 '19 at 16:04

1 Answers1

0

To answer your question I will use an example:

list = [1,2,3,4,5]

As you said, the first brackets will reverse your list. Then, you will get as a result: [5,4,3,2,1]

On that list you will do the slicing: [:-1]. That will give you as a result [5,4,3,2].

The brackets meaning is the same: [<startIndex>:<endIndex>:<how to go through>]

For more information about the <how to go through> part, please read here: https://www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/

Amit Haim
  • 273
  • 1
  • 3
  • 14