Let's talk about the append method of a list and how it works. Possibly one of the most common and therefore, most important concepts to learn about lists.
In case you missed it, here are the previous parts of the series
0:00 All right, so, in this video,
0:00 we are going to talk about how append works in a list,
0:04 because the last video got a little bit long,
0:06 but for those of you who want their minds blown,
0:09 I want to even give you a better solution
0:10 for the problem with the whole splitting thing
0:13 and then storing it as a product, price and condition.
0:17 So remember, we had data:
0:19 XBOX 360, product;
0:20 150, price; condition, new.
0:23 There's even an easier way to do it.
0:24 I can say product, I can say comma price,
0:28 and I can say condition, and check this magic out.
0:32 And I'm gonna say data.split that by pipe,
0:38 and boom, we're done.
0:40 So now product is the first element from that list, right?
0:44 If I show you data.split...
0:48 By pipe...
0:50 What you get is, first element,
0:52 second element, third element.
0:54 So the first element maps to this product right...
0:57 This product right there,
0:59 price ends up mapping itself to 150,
1:02 condition ends up mapping itself to new.
1:04 So go check it out, it's also kind of known
1:07 as the concept of tuple unpacking.
1:10 Really, really advanced concept,
1:12 we're not gonna get to it 'til maybe
1:13 an intermediate or advanced course,
1:16 but yeah, just something a little fun
1:19 to blow your mind, and look at how efficient
1:21 Python lets you do things, right, efficiently.
1:24 So, really simple way.
1:27 But hopefully this shows you the power
1:29 of how you can combine lists
1:31 with the whole idea of slicing and all that.
1:33 Now, one of the common, most common things I wanna show you
1:36 is this whole thing called .append, okay?
1:40 It's a method that you can use on a list.
1:41 Very common method, and this is one of the reasons
1:44 why lists are a lot more useful than strings
1:46 or any other thing.
1:50 Let me just be clear.
1:51 It's not that strings are not useful,
1:53 or lists are more useful,
1:55 but the reason why lists are so awesome
1:58 is because of that, that's what makes them unique.
2:01 So, let me give you some list with...
2:07 Sure, let's give you racers, or,
2:10 let's do something simple.
2:11 Let's do numbers.
2:15 Now, there's a really easy way to grow this list,
2:18 so I wanna keep adding some numbers to this list.
2:20 Well, how do I do that?
2:22 What if I wanna add some number to this list?
2:24 I know how to access each of these numbers.
2:26 I know how to slice each of these numbers and get a range.
2:29 I know how to step and slice
2:31 and give ranges at the same time.
2:32 I know how to reverse the list.
2:34 Well, what if I wanna add more things into the list?
2:38 Or change existing things in the list?
2:42 To add things in the list, we use .append.
2:48 Meaning add something to the end, hence append.
2:51 Pre-pend, to add something to the beginning.
2:53 So, we wanna add something right over here
2:56 in front of the five.
2:57 So lemme do six, and if I show you numbers,
3:01 now you have a bigger list.
3:03 Let's say I wanted to add another thing.
3:05 I'll just say append(7).
3:07 Now if I show you numbers, now the numbers has grown bigger.
3:09 You got seven.
3:11 All right, so, we're using this new app
3:13 that you're creating where once your friends raced,
3:16 you put them in list,
3:17 but let's say more of your friends get excited,
3:19 they're like, wow, this app is amazing,
3:21 we wanna race too so you can put us in there,
3:23 but you're like, I already...
3:25 The list only takes in three people.
3:28 Append lets you add in more people, right, so you store
3:31 that data, you keep it there, and then you use append
3:33 to add more of your friends into the list.
3:37 What's also nicer is you can use a loop
3:39 to add things into the list, okay?
3:42 So for example, I can say, for i in range(100),
3:46 if I say print i, well...
3:48 What is that gonna do?
3:49 It's gonna go all the way and print up to 100, okay?
3:52 So I'm gonna stop this right here,
3:54 but you get the point.
3:56 It's basically...
3:58 Range, right, what is range?
4:00 Just to show you range of 10,
4:03 that's effectively just a list, 10, okay,
4:07 and then we kind of go through it
4:10 and we print out each of those numbers.
4:12 I'm gonna get more into loops in the later video,
4:15 but just, this is just to give you guys
4:17 a little bit of an idea of how we use lists.
4:21 So, for example, every time i...
4:24 so the first time, i is zero, then i is one, i is two,
4:26 then i is three, and we keep doing that, right?
4:30 So, what can I say?
4:32 I can say...
4:35 'Cause, effectively, right, since this is a list,
4:39 now I can explain to you guys a little more about loops,
4:42 since range is really just a list, right?
4:46 I'm calling list on it 'cause in Python 3.0
4:48 they hide it from you, and you'll learn why later,
4:52 but, if I call list on it,
4:53 it shows me what range(10) looks like,
4:55 and range(10) happens to just be a list,
4:58 and a list is what allows you to make that for loop, okay?
5:02 That you've been making all when you were creating
5:04 your original turtle games.
5:06 So what happened is that...
5:08 The first time going through this list, right,
5:12 let's say, let's just make a smaller list here for now.
5:15 When we're going through the loop, i is zero the first time.
5:19 And then we call the print function right here, right?
5:23 So i is zero the first time, and then we print zero.
5:27 Then i is one, because it's the second element
5:30 from the list, and so then we print one out to the screen,
5:34 then i is two.
5:36 And then you get two here,
5:38 and then we print out two at the bottom, okay?
5:41 So that's how loop runs, right?
5:43 The indented code keeps running over and over again.
5:47 So, that's really what allows you do to a loop.
5:50 It's actually a secret list in the background,
5:53 and so that's why you go from zero to 100 here, right?
5:57 Now, if I wanted to take that list that I had
6:00 at the top called numbers and keep adding
6:02 a lot of numbers to it, say, up to 30,
6:06 instead of keep having to manually do it,
6:08 well, here's a nice way to do it.
6:09 I can say for i in range...
6:13 Let's see, 14, or...
6:16 Let's say, or...
6:18 I wanna start my range, okay...
6:22 So if I just showed you,
6:23 you can also give a start and a stop to a range,
6:26 so let's say I wanna start my range from eight
6:30 and go to up to but not including 20, or 21,
6:34 so I go from eight to 20, right?
6:36 So these are the numbers I wanna add, okay?
6:39 So let's do for number in range...
6:45 Eight and 21,
6:47 and I'm not using i, I'm using the variable number,
6:49 just so you guys know that you can...
6:51 You don't have to use...
6:52 You know, i is not some magical built-in things
6:55 that you have to use.
6:56 Use a variable that makes more sense.
6:58 In this case, since I'm going through looping,
7:00 and it's really numbers, and now you guys know
7:02 that i turns into a number,
7:04 it just makes more sense to call it a number.
7:06 So then you're like, oh yeah,
7:08 this will just be eight the first time,
7:10 then it'll be nine, then it'll be 10,
7:11 then it'll be 11, and then it'll be 12, right?
7:14 So let's go back...
7:17 So, let's replace this with just number,
7:20 and then let's say numbers.append,
7:24 and to the numbers list, I wanna keep appending...
7:29 Each number.
7:32 So the first time I'm gonna append,
7:35 number's gonna be eight, so then I'm gonna append eight.
7:38 And so it's gonna add eight to my numbers list.
7:41 Then numbers, number, is going to be nine,
7:46 and then it's going to append nine here.
7:48 Then number is going to be 10, and it's going to append 10.
7:53 So let's do it like this,
7:54 let's replace this bad boy with number,
7:57 let's run it,
7:58 let's take a look at our list,
8:00 and, voilà, you see it goes from one
8:04 all the way to 20.
8:10 If these were your friends and we wanted to see...
8:14 You know, in the reverse order, like,
8:16 from losers to winners,
8:18 instead of winners to losers in a race,
8:21 I would be able to do simply this trick,
8:25 which says start by default, stop by default,
8:29 and then step by negative one.
8:31 So, that answers that question, right?
8:38 So a lot of stuff, but hopefully it's making sense,
8:41 and hopefully...
8:43 Lists are making more sense,
8:45 and append is making more sense, right?
8:47 Append is one of the most useful things.
8:50 So I want you to remember append,
8:52 and we'll come back to it,
8:54 and we'll probably build more things with it, okay?
8:56 I'll have a project for you guys as well
8:59 where you're gonna get to practice it more.
9:01 For now, come up with something cool,
9:04 whatever silly idea or fun idea that you have,
9:08 code it up, something simple,
9:10 post it in the comments below,
9:12 in the YouTube one or my website here, okay?
9:18 Either way, that's fine.
9:19 All right, guys, I'll see you in the next video.