How python slicing [::-1] (colon colon minus one) reverse the sequence?

Durai
May 11, 2022

--

Slicing syntax: sequence[start:end:step]

If start and end omitted with POSITIVE step value:

  • start = 0
  • end = len(string)

Example:

- list1 = [1,2,3,4,5]

- list1[::2] => list1[0:6:2] => [1, 3, 5]

If start and end omitted with NEGATIVE step value:

  • start = len(string)-1
  • end = -len(string)-1

Example:

- list1 = [1,2,3,4,5]

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

--

--

Durai
Durai

Written by Durai

Software Engineer | Linux | Shell | Python

No responses yet