Sequences (lists, tuples, strings)

Coding with Python

🕑 This lesson will take about 20 minutes

In this lesson, we will start looking at sequences. In Python, a sequence is an ordered collection of items or elements. Sequences are one of the fundamental data types, and they provide a way to organise and work with data in a specific order.

The three main types of sequences in Python are:

  • lists

  • tuples

  • strings

Elements in a sequence are accessed using indices. The index starts at 0 for the first element, 1 for the second element, 2 for the third element, and so on. For example, if you have a list of names and want to get the third name in the list, you could reference it using the index of 2.

Lists

A list is exactly what it sounds like. A list is a sequence of data elements, for example, a list of people’s names, or a list of scores, or a list or products. Lists are mutable sequences which means that you can modify their elements (eg. modify or replace an item in a list with a new item, or add or remove items from a list). Lists are defined using square brackets [] and can contain elements of different data types (eg. a mix of strings, integers, floats, or booleans).

To make a list in Python, firstly you need to give it a name. A list could be called family for example, and store the names of members of a family. Let’s use a family as an example for creating a list. Making a list is similar to making a variable. Firstly, give the list a unique name, followed by the equals ( = ) sign. After that, specify the elements of the list between square brackets, and separate each element by a comma. If the elements are strings then they also need to each be inside quotation marks.

In the example below, we have a list called family with some elements in the list ("Homer", "Marge", etc.). To reference a specific element (value) in a list, we can specify the index of the element we’d like to access. For example, family[2] would be referencing the third element in the list below which stores the value “Bart”.

Once you create your list, Python automatically numbers (indexes) each element in your list starting from 0. In the example above, Homer would be [0], Marge would be [1], Bart would be [2], Lisa would be [3], and Maggie would be [4]. Even though there are 5 elements in the list, indexing only goes up to 4 because indexing the list starts from 0 instead of 1. This is known as zero-indexing.

A diagram showing that the value Homer has an index of 0, Marge has an index of 1, Bart has an index of 2, etc.

Say you have a long list and you want to return an element from the list that is near or at the end of the list. You donʼt need to count from left to right all the way through the list. Python can also count backwards. The last element in your list is also indexed as -1 and the second last element in your list is -2 and it keeps going back -3, -4, -5 and so on, depending on how big your list is.

For example, to reference “Maggie” in the example list, you could either write family[4] or family[-1] .

It’s also important to remember that Python allows you to store elements of different data types in lists. Many programming languages use arrays (which are like lists) to store a sequence of elements. In many of these languages, arrays don’t support mixed data types (in other words, all elements in the array must be of the same data type eg. all strings, or all integers, or all floats, etc).

Here is an example of a list in Python containing values of different data types. This example code also shows how to modify existing elements in a list and how to add more elements (append) to an existing list.

Tuples

Tuples are similar to lists except that they are immutable sequences, meaning that once they are created, their elements cannot be changed. Like lists, tuples can also contain elements of different data types. Tuples are defined using parentheses () instead of square brackets. You cannot modify, add, or delete elements from a tuple like you can with a list. You’ll receive an error message (“TypeError: 'tuple' object does not support item assignment”) if you try it!

Check out the example of a tuple below. Notice how the values are contained within parentheses instead of brackets?

Strings

It might come as a surprise that strings are also a type of sequence. They are indexed in the same way as a list or tuple. For example, in the string “strawberry” the letter ‘wʼ has an index of [4] (remember, indexing starts from 0). However, not all methods that work on lists will necessarily work on strings. Lists, tuples, and strings have different methods available to use in the Python language for specific uses (such as replacing or removing elements, appending, joining lists/strings, etc).

Here is an example of a string as a type of sequence in Python:

Multi-dimension lists

A multi-dimension list in Python is essentially a list containing lists (or a list of lists of lists, and so on). Each level of lists nested within a list is called a dimension, for example:

  • A list of lists is called a two-dimensional (2D) list

  • A list of lists of lists is called a three-dimensional (3D) list

  • and so on…

With a 2D list, we can imagine it as a table with “rows” and “columns”. Each list within the list would be a row, and each element within a row would be a cell/field that where a row and column intersect. To create a 2D list, we essentially create a list eg. my_list = [] and then within the square brackets, we create more lists containing a set of values within square brackets, where each list is separated by a comma.

Here is some sample code that demonstrates how to create a two-dimensional list (list of lists) and then access elements within it. To access elements in this scenario, we first identify the index of the first dimension (the list within the list) within square brackets, followed by the index of the target element in that list within square brackets. For example, if we wish to access the second list and its third element, we would write my_list[1][2] , where [1] is the index of the list within the list, and [2] is the index of the target element (remember, indexing begins at 0). In the example 2D list below, we would get the value 6.

Review questions

  1. What kind of scenario would a programmer prefer to use a tuple instead of a list?

Next lesson: Slicing