Work With Lists
00:00 Now that you know how to create a list, let’s look a little at how you can work with lists. You’ll start off by revisiting what tuples do and how lists are similar.
00:10 In that sense, you can do indexing with lists just in the same way that you can do it with tuples. You use the square brackets and give the zero-based index of the element to get back out the element.
00:23 You can do slicing, which means that you can again work with the indices of the elements in the list and you can define a slice and you’ll receive that slice back as a list.
00:36
Then you can do membership testing in the same way that you can do it with tuples. You can use the in
keyword to check whether an element is inside of the list.
00:47
And finally, you can also use iteration on lists. You can use a for
loop to loop over a list and then do something with each element in that list.
00:56 Let’s try out these four things.
01:01
I will again create a list of numbers, [1, 2, 3]
, and then we wanted to start off with indexing. For example, the first position is going to be number 1
.
01:16
So if I write numbers[0]
, and then close the square brackets, then I get out the number 1
because this is the first element in this numbers
list, and I can use slicing.
01:26
So I can say numbers
starting from, for example, the first index up to, but not including, the third index. So I can do this even though there is no index three in this list. We just have zero, one, and two.
01:42
But if I put three here, then it’s going to include both the element at index one and at index two. So numbers[1:3]
, gives me as a result the list that contains the elements two and three.
01:59 There’s actually a shortcut for this that works the same way as with indexing and slicing on strings that you’ve learned about before. You can leave out the final one if it goes all the way to the end.
02:11
So you can say numbers[1:]
, and then close the square brackets. So you’re just omitting the end index, which is outside of the list already. And in this case, it also gives you back the elements 2
and 3
.
02:26
Okay, so that’s slicing. You can also use the membership testing. So I can say, for example, the number 2
is in numbers
. Python returns True
in that case, because 2
is an element of this numbers
list, but if I do something like "Bob" in numbers
is the string "Bob"
in the list numbers
?
02:46
Then I’m going to get False
back because that string is not part of this specific list. Of course, it could be in there because lists can contain all data types, but in this case, "Bob"
is not in numbers
.
02:59
And finally, iteration. You can use loops to iterate over lists just like you could with tuples. So I can say for number in numbers
,
03:10
and now let’s do a little thing here. If the number % 2 == 0
, so if it can be divided by two without leaving any remainder, is what I’m saying here by typing if number % 2 == 0
, so that means that it’s an even number, then I want to print out the number. Oops.
03:36
Right? And that for
loop gives me 2
because that’s the only even element in this short list. So if the list of numbers had an additional number, 4
, let’s make a quick [1, 2, 3, 4]
,
03:52
and then I do the loop again, for number in numbers
if number % 2 == 0
, then print out number
, and now I’m going to get 2
and 4
as a result, because now the numbers
list contains two even numbers.
04:14 Okay, so this is how you can work with lists in the same way that you can work with tuples. All of these things work the same way,
04:22 and in the next lesson you’re going to look at how they’re different because what you can do with a mutable collection that the list is in Python is you can change elements.
04:32 And that’s something you can’t do with tuples, which gives you specific power and flexibility when you’re working with lists. Let’s look at that in the next lesson.
Become a Member to join the conversation.