Slicer
Slicer (source: Hans via Pixabay)

In this tutorial, we will review the Python slice notation, and you will learn how to effectively use it. Slicing is used to retrieve a subset of values.

The basic slicing technique is to define the starting point, the stopping point, and the step size - also known as stride.

We will review the basic way to retrieve data from a list, as well as some more advanced techniques.

First, we will create a list of values to use in our slicing.

# Create two lists to slice, the first is a numeric list from 1 to 9 (List A).
# The second is also a numeric list, from 0 to 9 (List B)

A = list(range(1,10,1)) # start,stop,step
B = list(range(9))

print("This is List A:",A)
print("This is List B:",B)
# Index the number 3 from A and the number 6 from B.
print(A[2])
print(B[6])

Basic Slicing

Extended indexing syntax used for slicing is aList[start:stop:step]. The start argument and the step argument both default to none - the only required argument is stop. Did you notice this is similar to how range was used to define lists A and B? This is because the slice object represents the set of indices specified by range(start, stop, step). Python 3.4 documentation

As you can see, defining only stop returns one element. Since the start defaults to none, this translates into retrieving only one element.

It is important to note, the first element is index 0, NOT index 1. This is why we are using 2 lists for this exercise. List A's elements are numbered according to the ordinal position (the first element is 1, the second element is 2, etc) 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.

A[:]

To retrieve a subset of elements, the start and stop positions need to be defined.

Given the pattern aList[start:stop], retrieve the first two elements from List A. If you are having trouble, you can see the solution below the exercise.

For all of these exercises, write your code in the box provided under the exercise description and click "Run" to test the output.

Exercise 1: Retrieve the first two elements from List A


Solution

print("The first two elements of A:",A[0:2], A[:2])

Notice two things here. First note, although we indexed with 0:2 which expands out to [0 1 2], only 2 elements are returned. Why? Think of stop as a stop sign. You stop before the stop sign, not at it or after it. [0:2] is putting the stop sign at index 2, which is the third element. Therefore, the elements before the stop sign are returned. Second note, when no start is defined as in A[:2], it defaults to 0.

There are two ends to the list: the beginning where index=0 (the first element) and the end where index=highest value (the last element). Beginning/end can also be thought of as top/bottom or front/back.

There are also two directions to move through the list (from first to last element and from last to first element). Until now, we have been moving from the first to the last (the starting point being index=0 and moving towards the last element with positve numbers). What about moving in the opposite direction through the list, from the last to the first element? Direction is indicated with a positive or negative number.

Given that a negative number indicates that the last element is the starting point, try retrieving the last element of B with a negative number. (Hint: -0 is not valid).

Exercise 2: Retrieve the last element of B with a negative number

Solution
B[-1]

Now that you know how to define a range and how to index in two directions, retrieve the numbers 4,5 and 6 from A in two ways:

Exercise 3: Retrieve 4,5,6 from A in two ways: using postive and negative number indices

Solution
print("Reading from left: ", A[3:6])
print("Reading from right: ", A[-6:-3])

But what if you wanted to retrieve the numbers in reverse order as 6,5,4? This is where the step parameter comes in very handy.

The step parameter indicates how big the stride is. For example, try going from default start to default stop in steps of 2 through List A. Remember the pattern [start:stop:step] and, default start and default stop simply means not specifying them.

Exercise 4: Go through list A from beginning to end in steps of 2

Solution
print("Going through A in steps of 2 starting from first to last element: ", A[::2])

Now go through list A in the opposite direction (from last to first element) in steps of 2. Remember that negative numbers indicate whether the first or the last element is the starting point for indexing.

Exercise 5: Go through list A from the last to the first element in steps of 2

Solution
print("Going through A in steps of 2 starting from the last element: ",A[::-2])

Now you have the skills to select [6,5,4] from list A. Remember that start and stop is relative to the direction you travel through the list.

Exercise 6: Select [6,5,4] from list A

Solution
print(A[5:2:-1])
print(A[-4:-7:-1])

As I mentioned before, start and stop is relative to the direction you are moving in. Therefore, Exercise 7 has two solutions. Starting at index 5 and stopping at index 2 or starting at index -4 and stopping at index -7. However, both read from the end to the beginning, as indicated with the -1 step parameter.

Given Exercise 6, why does the following return an empty list?

A[5:2:1]

This syntax is saying move from 5 to 2, which is right to left, in steps of one going left to right. In other words, you can't get to a position on the left by moving right.

Slicing also works on strings. Using what you learned above, try the exercises below.

S = 'This is a string.'
Exercise 7: Select 'This' from S. Be sure to run the cell S = 'This is a string.' first.

Solution
S[:4]
Exercise 8: Select every third character from S, returning them in reverse order.

Solution
S[len(S)::-3]
Exercise 9: Change the list of colors to [red, orange, yellow, green, blue, indigo, violet].
colors = ['red','orange','yellow','green','blue','purple']

Solution
print(colors)
colors[5:] = ['indigo','violet']
print(colors)

A side note: Although the above solution works, when adding to the end of a list, check out the methods aList.append() or aList.extend(). + and += can also be used. Python 3 documentation

SUMMARY

Slicing is a way of getting subsets of data structures. The basic notation is: [start:stop:step] The default for start is none or 0. The default stop is the end of your data structure. Using a positive number references from the first element, a negative number references from last element in your structure.

Appendix

Python 2 vs Python 3

A few notes about the syntax used in this tutorial:

A = list(range(1,10,1))

range() in Python2 creates a list to iterate through, which can take up a lot of memory depending on the size. range() in Python3 is like xrange() in Python2. It creates a generator to iterate over, meaning it takes up space in memory as you step through it, not all at once. Therefore, to create the list in Python3, the list function is wrapped around the range function.

print(A)

The print statement in Python2 print A has been replaced with the print function in Python3 print(A).

Article image: Slicer (source: Hans via Pixabay).