Slicing is an incredibly useful and powerful feature of python. It gives you to ability to manipulate sequences with simple and concise syntax. Slicing has more uses than I can think of or list here, but some of many useful applications include string manipulation and various mathematical uses; when using NumPy you will encounter slicing a lot.
Slicing has incredibly simple syntax; after a sequence type variable inside square brackets you define a start point, an end point and a step, separated by colons like so; var[start:end:step]. However it doesn’t work the way you might first assume it to. For example if you choose the starting point to be 0 and the end point to be 5, the slice will include indexes 0, 1, 2, 3 and 4. The index 5 will not be included.
One way to remember this is that the start point is inclusive and the end point is exclusive. In other words the slice will be from the start point up to but not including the end point. For the more visual folks another way is to to picture a wall before each value in the sequence. Then the start and end points reference these walls, not the values behind them. The slice will be between these walls, which of course will not include the value behind the end wall because we only go up to the wall and don’t pass it.
The diagram above shows how to picture sequence indexes and the values they reference. Thinking this way will make understanding slicing a lot easier.
A BASIC SLICING EXAMPLE
Throughout this post we will be using the variables defined in the below example but to avoid clutter they will only be included in this first example. The output of each statement will be included in a comment after it starting with ‘>>>’.
l = [23, 42, 96, 7, 84, 99, 54, 1]
t = (2, 85, 64, 129, 92, 84, 1, 33)
s = 'Hello internet!'
# Basic slicing.
print(l[4:7]) # >>>[84, 99, 54]
print(t[4:7]) # >>>(92, 84, 1)
print(s[4:7]) # >>> 'o i'
The above lines of code take a slice from the 4th element in the sequence up to but not including the 7th element. Note that strings can be sliced as well as lists and tuples.
OMITTING START/END POINTS
When specifying a slice you can omit start and end points. What this does is set the start point to 0 if you omit the start point and set the end point to end of the sequence, including the last item if you omit the end point.
# First five.
print(l[:5]) # >>>[23, 42, 96, 7, 84]
print(t[:5]) # >>>(2, 85, 64, 129, 92)
print(s[:5]) # >>>'>Hello'
The above examples makes a slice of the first 5 elements in the sequence. They omit the start point of the slice and so therefore start from the beginning.
# Last five.
print(l[-5:]) # >>>[7, 84, 99, 54, 1]
print(t[-5:]) # >>>(129, 92, 84, 1, 33)
print(s[-5:]) # >>>'rnet!'
This example does the opposite and makes a slice of the last 5 elements. This time the end point is omitted and so the slice is taken to the end of the sequence.
Another useful feature of slicing is stepping. The step of your slice defines how it ‘steps’ through the sequence your are slicing. For example a step of 5 would make it jump five elements with each step and a step of -1 would make it step backwards through the sequence.
# Every other.
print(l[::2]) # >>>[23, 96, 84, 54]
print(t[::2]) # >>>(2, 64, 92, 1)
print(s[::2]) # >>>Hloitre!
The above example defines a step of 2 which produces a slice of every other element; every evenly indexed element. If you wanted to take a slice of every oddly indexed element then you would input 1 as the start point.
# Reverse order.
print(l[::-1]) # >>>[1, 54, 99, 84, 7, 96, 42, 23]
print(t[::-1]) # >>>(33, 1, 84, 92, 129, 64, 85, 2)
print(s[::-1]) # >>>!tenretni olleH
This example reverses through the sequence and produces a slice containing every element but in reverse order.
Note that in the two above examples both the start and end point of the slice is omitted. This produces a slice of the whole sequence from start to finish. Normally this would be useless, however when combined with a step it provides a useful way of manipulating a whole sequence.
And that’s it for slicing! Python’s slicing is an incredibly useful function but also incredibly easy to use once you have understood the way that indexes are used. Hopefully now you are well on your way to ninja level slicing skills in Python.
Make sure you share this post so others can read it as well and don’t forget to follow me on Twitter, add me on Google+ and subscribe to this blog by email to make sure you don’t miss any posts that are useful to you.