AppDividend
Latest Code Tutorials

What Is Python Slice Notation | Python Slice Notation Tutorial Example

0

What Is Python Slice Notation | Python Slice Notation Tutorial Example is today’s topic. Python slice() constructor creates the slice object representing the set of indices specified by range(start, stop, step). In this tutorial, we will review the Python slice notation, and you will learn how to use it effectively. Slicing is used to retrieve the subset of values. The basic slicing technique is to define a starting point, the stopping point, and the step size – also known as a stride.

Python Slice Notation Tutorial Example

First, we will create list of values.

# app.py

A = list(range(2,10,2))
B = list(range(9))

print("List A:",A)
print("List B:",B)

The output is following.

 

Python Slice Notation Tutorial Example

Basic Slicing in Python

Extended indexing syntax used for slicing is following.

List[start:stop:step]

The start argument and the step argument both default to none – the only required case is stop. Did you notice this is similar to how the range was used to define lists A and B? It is because the slice object represents the set of indices specified by range(start: stop: step).

It is important to note, and the first element is index 0, NOT index 1. This is why we are using two lists for this exercise. List A’s elements are numbered according to the ordinal position while List B’s elements are the numbers that would be used to index them ([0] for the first element 0, etc.).

With extended indexing syntax, we retrieve a range of values. For example, all values are retrieved with a colon. Now, let’s do some examples.

Retrieve the first two elements from List A

Let’s get the first three elements of the list using the extended slicing syntax.

# app.py

A = list(range(2,10,2))
B = list(range(9))

print("The first three elements of A:",A[0:3], A[:3])

In the above code, we first we have written the A[0:3] which means from index 0 to index 2. So basically, it includes index 0, 1, 2. In our case, it is 2, 4, and 6. So the output would be 2,4,6

In the second case, we have written A[:3]. It means that the first three elements which have index 0, 1, 2.

So, the output would be the same as the first case. See the output below.

 

Retrieve the first two elements from List A

Retrieve the last element of B List with a negative number

# app.py

A = list(range(2,10,2))
B = list(range(9))

print("The last element of B:", B[-1])

The output is following.

 

Retrieve the last element of B List with a negative number

In the List, the index is starting from 0. Also, if we count from the last element of the list then the index is starting from -1. So, in the list, the last element’s index is always -1. So, in the above example, the last element of List B is 8 and also remember that the list starts from 0.

Now, also let’s take another example and reading from left. See the following code.

# app.py

A = list(range(2,10,2))
B = list(range(9))

print("The last elements of B:", B[-7: -2])
print("The last elements of A:", A[-4:-1])

The output is following.

 

Understanding slice notation

Understanding slice notation in Python

Summary of slice notation is following.

a[start:stop]  # elements start through stop-1
a[start:]      # elements start through the rest of the list
a[:stop]       # elements from the beginning through stop-1
a[:]           # a copy of the whole list

There is also a step value, which can be used with any of the above.

a[start:stop:step]

The critical point to remember is that a: stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements chosen (if the step is 1, the default).

The other feature is that start or stop may be a negative number, which means it counts from the end of the array instead of the beginning.

a[-1]    # last item in the list
a[-2:]   # last two items in the list
a[:-2]   # everything except the last two items

Similarly, step may be a negative number.

a[::-1]    # all items in the list, reversed
a[1::-1]   # the first two items, reversed
a[:-3:-1]  # the last two items, reversed
a[-3::-1]  # everything except the last two items, reversed

The ASCII art diagram is helpful too for remembering how slices work.

 +---+---+---+---+---+---+
 | k | r | u | n | a | l |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1

Finally, What Is Python Slice Notation | Python Slice Notation Tutorial Example.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.