[: -1] in Python

Vaibhav Vaibhav Oct 10, 2023
  1. Slicing in Python
  2. Significance of [ : -1] in Python
[: -1] in Python

Slicing is an operation in programming languages that we can perform over sequences of objects or arrays of objects or strings. In slicing, we slice a data structure using functions and simpler notations to get a substring or a subsequence, or a subarray of the original data structure.

Slicing in Python

Python is a programming language which means that it also supports slicing. In Python, there are primarily two ways in which we can perform slicing. The first uses the slice() function, and the second uses the indexing syntax.

Slicing in Python Using the slice() Constructor

The slice constructor accepts three arguments: start, stop, and step. This constructor creates a slicing object to split a string or any sequence of objects.

  • start: This is the starting index from where the slicing should begin. Its default value is 0.
  • stop: This is the ending index where the slicing should end. It is a compulsory argument.
  • step: This is the amount of increments that should be taken while iterating over the sequence for the process of slicing. Its default value is 1.

To understand this utility a little better, let’s look at some examples. Note that in Python, -1 represents the last index. And, while indexing or iterating, the stop index is not considered in the final result. Operations are performed over everything before it.

s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

slices = [
    slice(5),
    slice(0, -1),
    slice(1, -1, 2),
    slice(1, 4, 1),
    slice(5, -1),
    slice(3, -1, 3),
    slice(0, 0),
    slice(-1, 0, -1),
    slice(-5, 5, -3),
]

for i in range(len(slices)):
    print(s[slices[i]])

for i in range(len(slices)):
    print(a[slices[i]])

Output:

ABCDE
ABCDEFGHIJKLMNOPQRSTUVWXY
BDFHJLNPRTVX
BCD
FGHIJKLMNOPQRSTUVWXY
DGJMPSVY

ZYXWVUTSRQPONMLKJIHGFEDCB
VSPMJG
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 4, 6, 8]
[2, 3, 4]
[6, 7, 8, 9]
[4, 7]
[]
[10, 9, 8, 7, 6, 5, 4, 3, 2]
[]

Note that when a single value is passed to the slice() constructor, the value is assigned to the stop index. That is why for slice(5), we got the output as ABCDE.

Slicing in Python Using the Indexing Syntax

In Python, instead of using the slice() constructor to create slicing objects, we can use the following syntax to perform slicing.

sequence[start:stop:step]

The accepted arguments are the same as that of the former method; what really changes is the way of writing them. The default values for start, stop, and step is 0, -1, and 1, respectively. Note that we can skip the argument to use the default value, but it is necessary to write the colons. Let us look at some examples of indexing syntax to understand it a little better.

s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(s[5])
print(s[:-1])
print(s[0:-1])
print(s[1:-1:2])
print(s[1:4:1])
print(s[5:-1])
print(s[3:-1:3])
print(s[:0])
print(s[-1:0:-1])
print(s[-5:5:-3])
print(a[5])
print(a[:-1])
print(a[0:-1])
print(a[1:-1:2])
print(a[1:4:1])
print(a[5:-1])
print(a[3:-1:3])
print(a[:0])
print(a[-1:0:-1])
print(a[-5:5:-3])

Output:

F
ABCDEFGHIJKLMNOPQRSTUVWXY
ABCDEFGHIJKLMNOPQRSTUVWXY
BDFHJLNPRTVX
BCD
FGHIJKLMNOPQRSTUVWXY
DGJMPSVY

ZYXWVUTSRQPONMLKJIHGFEDCB
VSPMJG
6
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 4, 6, 8]
[2, 3, 4]
[6, 7, 8, 9]
[4, 7]
[]
[10, 9, 8, 7, 6, 5, 4, 3, 2]
[]

Significance of [ : -1] in Python

As we can see, this syntax belongs to the indexing syntax. [ : -1] will print all the sequence elements except the last one. Let look at some examples to understand this concept.

s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

print(s[:-1])
print(a[:-1])

Output:

ABCDEFGHIJKLMNOPQRSTUVWXY
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

Related Article - Python List