How to explain the first colon in the Python slice syntax list [:: - 1]

I recently read code snippets on how to change the sequence

>> l = [1,2,3,4,5,6]
>> print l[::-1]

Output

>> [6,5,4,3,2,1]

How to explain the first colon in parenthesis?

+4
source share
2 answers

Colons without predefined values ​​resort to default values. The default values ​​for the index start when the step is negative len(l), and the end index -len(l)-1. Thus, back slicing can be written as

l[len(l):-len(l)-1:-1]

which has the form.

l[start:end:step]

By extracting the default values, we can use it in a shorter notation like l[::-1].

It might be useful to consider this issue in the Python Snippet Designation .

+8
some_list[start:end:step]

- , . , : start - , 0, , end - , ( , ), , step - .

, , : " ".

EDIT: ,

[1,2,3,4,5,6][5:-7:-1]

,

[1,2,3,4,5,6][::-1]

Python3. - ? , ( ).

+2

Source: https://habr.com/ru/post/1534128/


All Articles