Retrieving Elements From a List
00:00 One of the benefits of lists is that due to their sequential structure, retrieving items by index is a highly efficient operation. So to access elements from an existing list, you can use a pair of square brackets.
00:12
Individual elements can be retrieved by their zero-based index using the syntax, my_list
with the integer index value in square brackets. Alternatively, you can retrieve multiple elements at once using a Python slice. This always returns a new list, and the syntax is my_list
, and in brackets, [start : stop : step]
.
00:35
start
indicates the first index value to retrieve, stop
is the non-inclusive endpoint, and step
is the gap between each element retrieved.
00:42
start
, stop
, and step
are all optional. This can make slices a little hard to read at first, but you’ll find it’s an incredibly useful technique.
00:51 Now let’s practice in the REPL.
00:54 Start by creating a list of five strings. In this case, five popular programming languages.
01:01
languages =
a list of the strings, "Python", "TypeScript", "C++", "Go",
and "Rust"
. Using square brackets, access the item at index zero and languages[0]
gives us the first element "Python"
. To go to the other end of the list, try languages[4]
, which gives us the fifth element "Rust"
.
01:22
And if you go a bit too far and try to access languages[5]
, you’ll get an IndexError: list index out of range
. You’ll get this error anytime you try to directly access an index that does not exist.
01:35 So be careful. You can also access indices in reverse using negative indexing. I like to think about it like walking backwards from the first element, index zero.
01:45
So index[-1]
returns the last element in the list, and so on. languages[-1]
returns "Rust"
, the last element in the list.
01:55
languages[-2]
returns the second last "Go"
, and on it goes ending in index -5
, languages[-5]
, which returns the first element of the list "Python"
, the equivalent of accessing index zero.
02:10 Positive indexing is good for when you know where something is relative to the start of a list. And negative indexing is good for when you want to get an item relative to the end of the list.
02:19 I’ll clear the screen for the next example.
02:22
And to explore slicing, start with a list of integers from zero to nine and call it digits
. If you want to save some typing, feel free to use the range()
trick that we saw in the last lesson.
02:33
To get the first three values, use square brackets with the slice :3
, digits[:3]
.
02:41 Remember, the syntax for a slice is start colon stop colon step, all in brackets. Omitting the start value defaults to starting at the beginning of the list and omitting the step value defaults to a step size of one. Setting stop as three tells Python to go as far as index three, but not include it.
03:00
And so this returns a list with the first three values of digits
: 0, 1,
and 2
. Conversely, you can use the slice digits[3:]
omitting stop and step.
03:13 When stop is omitted, the slice continues to the end of the list, including the last value. So this returns a new list starting with the item at index three, including all subsequent values.
03:24
To get the four middle elements, try digits[3:7]
, which returns the list 3, 4, 5,
and 6
.
03:34
Reverse indexing works as well. To get the last three digits, you can slice starting from index -3
, digits[-3:]
and the result is 7, 8,
and 9
.
03:45
And if you want to include step, you can grab every other element in the list by using the slice [::2]
. This works because by default the slice will start at the beginning and run to the end.
03:57
Because of the step size of two, the result is a list of even numbers from 0
to 8
. Can you think of how you might change this a bit to get odd numbers instead of even? Feel free to pause and try it out.
04:10
You can add a starting point of 1
and keep the step size of 2
to retrieve odd numbers digits[1::2]
, which results in a list of odd numbers from 1
to 9
.
04:23
Another thing to note about slices, if your start or endpoints are beyond the valid indices of the list, you won’t actually get an error. So if you try slicing from the start to 11
, for example, digits[:11]
, you get all 10 elements of the list, not 11.
04:42
And if you try to go from negative 11
onwards,
04:46
digits[-11:]
, the result is the same, all 10 elements of the list. And if you try to slice starting at an invalid positive index, like using the slice digits[11:]
, the result is simply an empty list.
05:01 And that was slicing lists, a powerful tool, but not without its quirks as you’ve seen. And there’s actually another common use of slicing, making a copy of a list.
05:10 You want to know how? Well, you’ll have to watch the next lesson to find out.
Become a Member to join the conversation.