#100DaysOfCode in Python Transcripts
Chapter: Days 7-9: Python Data Structures
Lecture: Manipulating Lists

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Lists are actually pretty simple. They're probably one of the things you're going to deal with the most in your Python journey
0:07 and the easiest way to demonstrate it is to just create one right here of numbers. So let's create a stereotypical list, all right.
0:18 We do that by using the square brackets and we're going to create five entries in that list. So this list contains five items.
0:30 Okay. Now, because we're dealing with numbers specifically, we don't have to put the quotes around it, okay.
0:39 If we were dealing with strings that's what we do, but we're dealing with just numbers so let's just leave it plain like that. So that's our numlist.
0:49 We'll just call it back so you can see what it looks like. There you go. So it's now a list of five numbers, okay. 1, 2, 3, 4, 5.
0:58 One of the cool things you can do with a list is you can actually reverse it. Okay. So, now need to write some nifty code
1:05 that will go through, parse it, and put all the values in back to front. We can just do numlist.reverse() Call numlist back and there you go,
1:17 5, 4, 3, 2, 1. Now we can do that again. Okay, and we're back to one, two, three, four, five. Now, if we actually go back,
1:35 one thing we can do, is we can actually sort the list. So numlist.sort(), okay. And there you go 1, 2, 3, 4, 5 again.
1:48 And this is very handy because you can actually sort with letters as well. Now let's say we want to actually print out all
1:55 of the values inside num list. How do we do that? We can use a four loop, okay? So we can go four num in numlist, print(str(num)).
2:09 So we hit enter and there are our five numbers, 1, 2, 3, 4, 5 So it's pretty powerful. There's with just this basic ideology you can get a lot
2:19 of stuff done in Python code. Now one of the other ways you can actually create a list is to call the list function against a certain string.
2:32 Let's say we have a string called - we have a variable called mystring. And we assign it to string Julian. Okay, so mystring is Julian.
2:45 Well how do we convert that into a list? We simply call list against it. So list(mystring) is Julian.
2:54 And there you go, you see my name has just been chopped up so that each letter, or each character I should say, is now a string value inside this list.
3:07 Right. So, what can we do? We can assign that so l = list(mystring). So we're assigning this here to the variable l, alright.
3:24 And we'll just call that back. And it's Julian. Now what are some interesting things we can do with this? Well, there's actually quite a lot.
3:31 We can actually reference the values by their position, by their index, inside that list. So we can go l[0] is J. We can go l[4] is A.
3:48 You can see we got J there, we got A there. Very handy. What else can we do? Well there are a few other functions we can call here.
3:56 We can go pop, and what pop will do it's actually going to return the last letter from this list. So, the letter N is going to be returned.
4:12 But at the same time, it's going to be removed from the list. So my name is now Julia. Right. We can then insert it back in.
4:24 And we use insert for that. Now when we insert, we actually, if you look at this tool tip here that's cheating,
4:34 you can see we have to specify an index and then the object. So what position are we inserting the letter N into this list?
4:44 Well, we're going to insert it into position 0, 1, 2, 3, 4, and 5 5 is going to be on the end, so position five. And what are we inserting?
4:56 We're inserting the letter N. All right, it's an actual string. Now when we call the list, there we go, it's rebuilt with the letter N.
5:08 Another interesting this we can do is we can actually replace any of these with any other letter.
5:16 So we can go l[0], which we know will actually return J, but we can replace J. So L[0] is going to be B. So, l is now Bulian.
5:36 Okay? Ya, a little play on words. Let's go with that. Now if we wanted to get rid of the B, we could actually delete it or we could pop it.
5:46 The difference is, as I've said before, pop will return the letter in that position, where as now with the delete option it will actually delete it.
5:59 You won't even know what you're deleting. It doesn't return anything, it just deletes. So if we want to delete that zero we just have to type del(l[0]
6:14 and there's the B gone. All right? Next we can do l.insert(). We'll choose index position zero. And this time we'll put an M in. l is now Mulian, okay.
6:32 And now, even better, with pop, let's say we do want to return something, we can go l.pop(), but now we can actually specify a position, an index.
6:44 So we can go l.pop(0), we get the M returned, and the M has also been removed from position 0 in the list. So those are some cool little nifty tricks
6:58 you can keep up your sleeve, add them to your book, because when it comes to lists, you'll end up dealing with them quite a lot.
7:04 So knowing how to pop and how to delete and insert and append, which is another one I'll show you quickly. l.append(), let's add S.
7:22 We can append, append will always add right at the end into the last position. So definitely keep all of these handy. You'll be using them quite a lot.


Talk Python's Mastodon Michael Kennedy's Mastodon