How to subset Python Lists

Learn how to access elements in a Python list using zero-based indexing, negative indexing for reverse access, and slicing with the colon syntax. The start index is inclusive, the end index is exclusive, so list[1:3] gets elements at positions 1 and 2.
Chapters
After you have created your very own Python list, you might wonder how you can access information in this list. Python uses the index to do this. Have a look at the family list again. Here. The first element in the list has index 0, the second element has index 1, and so on. Suppose that you want to select the height of Emma, so the float 1.68. It's the fourth element, so it has index three. To select it, you use three inside square brackets. Similarly, to select the string dad from the list, which is the seventh element, you'll need to put the index six inside square brackets. You can also count backwards using negative indexes. This is useful if you want to get some elements at the end of your list. To get your dad's height, for example, you'll need the index minus one. These are the negative indexes for all list elements. This means that this line and this line return the exact same result. Apart from indexing, there's also something called slicing, which allows you to select multiple elements from a list, thus creating a new list. You can do this by specifying a range using a colon. Let's first have another look at the list and then try this piece of code. Can you guess what it'll return? A list with the float 1.68, the string mon, and the float 1.71 corresponding to the fourth, fifth and sixth element in the list? Maybe. Let's see what the output is. Apparently, only the elements with index 3 and 4 get returned. The element with index 5 is not included. In general, this is the syntax. The index you specify before the colon, so where the slice starts is included, while the index you specify after the colon where the slice ends is not. With this in mind, can you tell what this call will return? You probably guessed correctly that this call gives you a list with three elements corresponding to the elements with index 1, 2 and 3 of the FAM list. You can also choose to just leave out the index before or after the colon. If you leave out the index where the slice should begin, you're telling Python to start the slice from index zero. Like this example, if you leave out the index where the slice should end, you include all elements up to and including the last element in the list, like here. Now it's time to head over to the exercises, where you will continue to work on the list you've created yourself before. You'll be using different subsetting methods to get exactly the piece of information you need.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







