Snake_Byte #29: Sequences, Slicing, and Steps

In this edition of Snake Bytes, we will discuss how slice notation is used to select elements from a sequence. In Python, a sequence is a datatype which contains multiple elements in a specific order. Common sequence datatypes include the list, tuple, and string.

First we will define a list used to store a name.

Our list is four elements long with separate elements for a title, first name, middle name, and last name.

Elements within a list may be accessed by index. In Python, list indices are zero-based, which is just a fancy way of saying that when we count, we start with zero rather than one. We can access each element within our list using its index.

To access the last element, we simply use a negative index to indicate that our index will be measured from the end of the sequence. The name[-1] instruction indicates that we want the first element from the end of the list.

The range, or indices, within our list is from 0 - 3. If we attempt to access an index outside of this range, Python will raise an IndexError.

Accessing a sequence's elements by index comes in handy when we need to access a specific element. However for cases where you need to generate a slice, or subset, of an existing sequence using single index access is tedious.

The preferred "Pythonic" approach is to use sequence slicing. The sequence slicing operation creates a new sequence containing a range of indices defined using bracket notation. In its simplest form, we can define slice notation using the following bracket notation:

[starting index : ending index]

There is one catch though! While starting index is the starting index, our slice will contain all elements up to, but not including, ending index. If starting index or ending index values are omitted they are defaulted to the appropriate starting and ending index values. Additionally, if ending index is outside of the sequence's range, an error is not raised. In this case the slice contains the elements up to and including the last entry. This is rather nice as you can focus on the matter at hand and don't need to insert conditional checks or exception handling within your parsing code.

Finally, we can extend our bracket notation to include a third parameter, step.

[starting index: ending index: step]

In this complete form the notation is used to start element section at starting index up to but not past ending index for every step element.

Sequence slicing is an effective means of generating a slice, or subset, of an existing sequence. The syntax is concise and readable, which leads to clean and effective code!

About dixonwhitmire

Dixon Whitmire, a software engineer for, is currently focused on delivering healthcare data and processing solutions on, helping to bring healthcare into the 21st century. He envisions a world where data is exchanged without XML or FTP, and curly braces are a thing of the past.

Visit My Website
View All Posts

Leave a Reply

Your email address will not be published.