Datatypes - Lists

Lists

Have you ever created a grocery list or a to-do list? In Python, such collections are aptly represented by a list.

A list is an ordered sequence of items with its index starting at 0.

True to Python's flexible nature, the elements in a list can be of any datatype. This means you can mix and match different datatypes, even including other lists within a list. Lists are fundamental tools in Python and may be created in two different ways. The first is made using square brackets [ ], which is the more common way. Another way is to use the list function. The next code editor showcases a variety of ways to make lists, as well as a variety of elements within them.

  • list01: is an empty list.
  • list02: is a list with two numbers.
  • list03: is a list with both numbers and text.
  • list04: is made with the list function and contains one element.
  • list05: is also made with list, and contains two elements.

We can determine the number of elements in a list using the built-in function len, which stands for "length". An empty list will have a length of zero, a list with one element will have a length of one, and so on and so forth.

Think of a book's table of contents. Each chapter or section has a specific page number associated with it. If we want to read a particular chapter, we look up its page number in the table of contents and then turn to that page. In this analogy, the page number is like an "index" that tells us where to find the chapter within the book.

Similarly, in programming, a list can be thought of as a book, and each item in the list is like a chapter. The "index" tells us the position of an item in the list, helping us to quickly access or modify it. Due to historical programming reasons, Python counts the first item in a list starting from 0. The second item is at index 1, and so on up until the last item in the list at index len(list) - 1. For example, in list03 in the first code editor, there are 4 items and thus the length of it is 4. Therefore, the last index in the list is 4 - 1 = 3. Another way to index a list is by counting in reverse; it will circle around to the end of the list and count backwards. So -1 will correspond to the last element in the list, -2 will correspond to the penultimate element, and so on. This index counting can be confusing at first, but it is prevalent throughout Python and many other programming languages so it is worth trying to become comfortable with.

We can also slice a list to subset it, using the colon symbol. The format is as follows a:b. The a is the start index. The b is the end index. A slice with indices 0:n would be the entire array. If we omit the a or b, then Python will automatically fill these in with values 0 and n, respectively.

Slicing in this way is inclusive at the beginning and exclusive at the end. In mathematical notation this is [a, b). For example, 0:10 will select the indices: 0, 1, ..., 9, but not 10. In the following code editor we include a few different ways we can use slicing to select elements in a list.

Now here's a cool trick. We can easily reverse a list via a double colon.

Lists are mutable, meaning, after it's created, we can continue to add or remove items from it.

To change an element in a list, use bracket notation to index into the list and use the assignment operator to assign a new value.

We will learn methods later when we learn classes. For now the short description of them is that they are functions that come bundled with an object. Let's look at some methods that can be used with any Python list.

To add to the end of a list, use the append method.

To remove an element in a list, use pop along with the index of where to pop from. If no argument is passed in, then the method will default to popping the last element in the list. When pop is used, it returns the element that is popped.

Practice Question

Given a list list01 = [9, 7, 5, 3, 1, -1], which of the following lines of code will NOT produce [3,5,7]?

score: 0%


Congratulations hopefully you will learn to appreciate how useful and powerful lists can be. We will next cover a similar concept, tuples.