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