1. Lists
So far, each variable we've worked with has contained a single value, whether that is an integer, float, boolean, or string.
2. Problem
However, it's common to work with many data points, such as lots of product prices. What if we need to store and work with more than one value?!
3. Lists to the rescue!
Python has a data type called a list, allowing us to store multiple values of any data type in a single variable.
We can create a list using square brackets or calling the list function, including the values to store inside, separating each value with a comma, such as here for product prices! We can also provide variables rather than the raw values.
4. Checking the data type
We can use Python's type function to check the type of a list variable.
The output confirms it is a list. This will be the case regardless of what type of data we store as values inside the list.
We can use the type function on any data type, like here where we check a string.
5. Accessing elements of a list
To check the value of a variable we print it.
However, as lists contain multiple values we might only want to check for one or more specific values, meaning that printing the entire list every time is inefficient.
Lists are ordered, so we can access certain elements by subsetting or indexing. Python does this by assigning an index number to each value in the list, where the first value has an index of zero.
6. Accessing elements of a list
We use square brackets to create a list. To subset we also use square brackets, inside which we provide the number of the index we want to subset.
Here, we provide zero to subset for the first element in our products list, which returns the corresponding value.
If we want the fourth element then we subset using square brackets and include three, returning the integer 15.
7. Finding the last element in a list
If we only want the final value in a list we can count to the end. In this case, it would be five as there are six elements and the index starts at zero. However, Python has a helpful trick so we don't need to count how many elements there are, which is helpful if we have a large list!
We can subset for the element at an index of minus one, which is shorthand for the last element.
8. Accessing multiple elements
Sometimes we might need to access multiple values from a list. We can extend our subsetting syntax to accomplish this!
We use square brackets to subset, passing the index of the first element, followed by a colon, and then the index of the last element plus one.
We add one because Python counts from up to but not including the index of the last element.
9. Accessing multiple elements
If we want all elements from a specific index to the end of the list, we again subset on the starting index and include a colon, but we don't provide a final number like so.
Likewise, if we want elements starting from the first index, we can leave the number before the colon is empty. Here, we get the first, second, and third elements, which are at indexes zero, one, and two, respectively.
10. Alternating access
We can also access every other element in a list. To do this, starting from the first element, we again start with a colon, but this time we include a second one, followed by the interval we want to use. That is, to grab every second element we put a two, returning the first, third, and fifth elements.
To start from the second element and get every third value, we can add a one at the beginning and change the two to a three, like so.
11. Lists cheat sheet
This cheat sheet summarizes the syntax for working with lists.
12. Let's practice!
Time to work with your own lists.