1 00:00:00,000 --> 00:00:02,700 Let's take a formal look at lists. 2 00:00:02,790 --> 00:00:07,160 So lists are, and we've covered this before, but it's like 3 00:00:07,170 --> 00:00:08,060 a grocery list. 4 00:00:08,060 --> 00:00:12,600 So you go for 'Milk', 'Bread', and 'Eggs', and you have 'Milk', 'Bread', and 'Eggs'. 5 00:00:12,690 --> 00:00:15,450 But in other programming languages they aren't called lists. 6 00:00:15,660 --> 00:00:19,260 They are called arrays, which are a lot more common. 7 00:00:19,270 --> 00:00:22,120 Python just uses its own terminology for some reason. But 8 00:00:22,130 --> 00:00:25,120 lists and arrays are essentially the same thing in most programming 9 00:00:25,120 --> 00:00:28,900 languages. So let's compare the difference between a list and a string. 10 00:00:28,900 --> 00:00:33,360 So we've got a 'lst =', and it can take any number 11 00:00:33,360 --> 00:00:36,600 of items, and it will preserve the order. 12 00:00:36,600 --> 00:00:39,200 So it'll always be [1, 2, 3, 4, 5] here, 13 00:00:39,210 --> 00:00:43,860 whereas a string can only ever have one value, 'st = "One value to 14 00:00:43,900 --> 00:00:45,700 rule them all". 15 00:00:45,700 --> 00:00:50,500 And a number or an integer can only ever be like 1 or 1.0. 16 00:00:51,500 --> 00:00:55,800 So a list is basically a bunch of strings, a bunch of numbers, 17 00:00:55,900 --> 00:00:58,620 a bunch of Booleans, all these different data types, 18 00:00:58,630 --> 00:01:00,600 you can put them into a list together. 19 00:01:00,610 --> 00:01:03,990 You can have lists inside of lists as well if you wanted 20 00:01:04,000 --> 00:01:06,840 to. So let's go ahead and take a look at the syntax of a 21 00:01:06,840 --> 00:01:11,500 list. 'my_friends'. Let's do 'my_cats'. 22 00:01:11,540 --> 00:01:14,990 So we've got a list here, and the syntax is basically the 23 00:01:15,000 --> 00:01:16,820 coding language, how this looks. 24 00:01:16,830 --> 00:01:19,490 So I've got a variable, standard variable, is equal to, 25 00:01:19,500 --> 00:01:20,960 so I'm assigning something, 26 00:01:21,100 --> 00:01:24,200 and this is how we create a new list in Python. 27 00:01:24,220 --> 00:01:27,160 In Python, this is just going to say, "Hey, there's a piece of 28 00:01:27,160 --> 00:01:30,500 memory, and it should be code named 'my_cats'." 29 00:01:31,060 --> 00:01:34,430 In here, I'm going to put my first item called 'Henry', and 30 00:01:34,440 --> 00:01:36,770 I'll put another one in here called 'Zephyr'. 31 00:01:36,950 --> 00:01:39,760 It starts with a hard bracket, ends with a hard bracket, '[]' 32 00:01:40,450 --> 00:01:43,840 These are strings, so these have to have quotations or apostrophes 33 00:01:43,840 --> 00:01:47,700 around them. And then between each item is simply a comma. 34 00:01:47,700 --> 00:01:51,240 The space is not really important, but if you add a space, 35 00:01:51,240 --> 00:01:52,700 it just seems to look a little nicer. 36 00:01:52,760 --> 00:01:54,800 So let's go ahead and see what we have here. 37 00:01:54,810 --> 00:01:57,230 Python thinks that 'my_cats' are '['Henry', 'Zephyr']'. 38 00:01:57,230 --> 00:02:00,100 And because it's hard brackets, you can immediately tell that 39 00:02:00,100 --> 00:02:01,300 that is a list. 40 00:02:01,300 --> 00:02:04,100 Also, you have two items here, so it looks like you've got 41 00:02:04,170 --> 00:02:06,230 one string, comma, another string. 42 00:02:06,460 --> 00:02:09,310 Just by reading that, you could figure out that it's not 43 00:02:09,310 --> 00:02:11,500 just a string, it's not just a number, it's a list. 44 00:02:11,560 --> 00:02:14,770 So when it comes to data science, then this may or may not 45 00:02:14,770 --> 00:02:16,800 be applicable to you, but in data science, you are going to 46 00:02:16,800 --> 00:02:18,400 see a lot of different lists. 47 00:02:18,400 --> 00:02:21,100 You're actually going to see lists of lists, which look crazy, 48 00:02:21,100 --> 00:02:26,000 but these build out to these things called matrices or matrices. 49 00:02:27,800 --> 00:02:30,300 So in here we've got an opening list, '[', and then we've got 50 00:02:30,800 --> 00:02:32,200 '1, 2, 3] 51 00:02:33,400 --> 00:02:37,200 ','; we've got another list in here, '[4, 5, 6]'. 52 00:02:37,200 --> 00:02:38,600 Let's add spaces, though, 53 00:02:40,200 --> 00:02:42,400 and '[7, 8, 9]'. 54 00:02:42,400 --> 00:02:45,200 And so when you look at this, this actually does sort of look like a grid. 55 00:02:45,250 --> 00:02:48,690 We've got a list of lists in here, and so when we loop through 56 00:02:48,700 --> 00:02:51,150 every item, we're going to get list number one, list number 57 00:02:51,150 --> 00:02:52,200 two, list number three. 58 00:02:52,230 --> 00:02:55,770 And then inside of each one of those, we would have [1, 2, 3]; 59 00:02:55,770 --> 00:02:59,100 in the second one we'll have [4, 5, 6]; and the third one we'll have [7, 8, 9]. 60 00:02:59,140 --> 00:03:01,510 Now, that's neither here nor there, and when you flatten 61 00:03:01,510 --> 00:03:04,000 it, it looks like this. It looks a little harder to read when it's flat. 62 00:03:04,080 --> 00:03:06,510 But in data science, you're going to see something like this 63 00:03:06,510 --> 00:03:07,900 fairly frequently. 64 00:03:07,960 --> 00:03:10,630 Now, if you're not going into the data science route, that's 65 00:03:10,630 --> 00:03:14,100 okay. It's still good to know that list can be nested inside of another list. 66 00:03:14,700 --> 00:03:17,300 Now, just going back a quick second here, I've got a variable. 67 00:03:17,310 --> 00:03:21,470 It's a list called 'Henry', a list called 'my_cats', with items 68 00:03:21,500 --> 00:03:23,500 called ['Henry', 'Zephyr'] in it. 69 00:03:23,540 --> 00:03:26,360 Now, lists are not quite the same as strings. 70 00:03:26,370 --> 00:03:28,640 They don't have all the same methods attached to it. 71 00:03:28,800 --> 00:03:33,700 If we do 'type(my_cats)', we're going to see that it's a Python 72 00:03:33,700 --> 00:03:36,800 'list'. 'my_cats.', and then hit tab, 73 00:03:36,870 --> 00:03:40,110 you're going to see all these different methods, things that 74 00:03:40,110 --> 00:03:42,200 we can do here. So I can 'append', 75 00:03:42,200 --> 00:03:43,400 I can 'clear', I can 'copy', 76 00:03:43,400 --> 00:03:45,900 I can 'count' the number of times something shows up, 77 00:03:45,900 --> 00:03:50,000 I can 'extend' it, 'index', 'insert' into it, 'pop', which is remove the last item, 78 00:03:50,090 --> 00:03:53,370 I can 'remove' something, I can 'reverse' it, and I can 'sort' 79 00:03:53,370 --> 00:03:56,600 it. All of these are pretty self explanatory for the most part. 80 00:03:56,600 --> 00:04:01,500 So if we just run 'reverse', and do 'my_cats', it's now 81 00:04:01,900 --> 00:04:02,830 the opposite. 82 00:04:02,830 --> 00:04:04,500 So it was originally '['Henry', 'Zephyr']', 83 00:04:04,500 --> 00:04:05,600 It is now '['Zephyr', 'Henry']'. 84 00:04:05,600 --> 00:04:09,800 But let's go ahead and do some actual Python coding 85 00:04:09,800 --> 00:04:12,000 or something that's going to be actually useful. 86 00:04:12,000 --> 00:04:15,900 So one thing we're going to do a lot as a Python developer, 87 00:04:15,900 --> 00:04:18,500 Python programmer, is we're going to add to our list, to my 88 00:04:18,500 --> 00:04:22,100 list has Henry and Zephyr in it, Zephyr and Henry in that proper 89 00:04:22,100 --> 00:04:24,100 order, and I'm going to append to it. 90 00:04:24,110 --> 00:04:27,220 Now, if you come from a JavaScript background that's called 91 00:04:27,220 --> 00:04:31,000 Push, in Python it's 'append', and I'm going to add a name in here. 92 00:04:31,100 --> 00:04:36,300 "Ezra". Now, it looks like nothing happened, but if I put 'my_cats' 93 00:04:36,300 --> 00:04:39,100 in here, you can see that it actually changed my variable, 94 00:04:39,140 --> 00:04:40,120 it added more to it. 95 00:04:40,200 --> 00:04:42,400 We didn't have to overwrite it, so we didn't have to do 96 00:04:42,400 --> 00:04:44,800 'my_cats = my_cats.append( 97 00:04:44,890 --> 00:04:49,100 "Ezra")'. We just had to do 'my_cats.append', and it automatically 98 00:04:49,110 --> 00:04:52,130 updated its internal listings, or its internal values for 99 00:04:52,140 --> 00:04:55,890 us. Now, let's say we wanted to remove "Henry". 100 00:04:55,900 --> 00:04:59,180 Not that I would ever want to, but let's say, just for funsies 101 00:04:59,210 --> 00:05:00,590 I wanted to remove "Henry", 102 00:05:00,600 --> 00:05:02,030 I could do 'my_cats', 103 00:05:02,040 --> 00:05:04,280 and the way I got 'remove' by the way, is just 'my_cats', 104 00:05:04,400 --> 00:05:05,400 that's the list, 105 00:05:05,420 --> 00:05:08,030 '.', and then 'remove', 106 00:05:08,800 --> 00:05:11,700 and let's remove the one item in here called 'Henry'. 107 00:05:13,100 --> 00:05:14,800 Again, it looks like nothing happened. 108 00:05:14,800 --> 00:05:19,800 But behind the scenes, Python said, "Okay, I understand you 109 00:05:19,800 --> 00:05:20,800 wanted to get rid of 'Henry'. 110 00:05:20,800 --> 00:05:21,900 That's what I did". 111 00:05:22,900 --> 00:05:26,200 Now, what do you think would happen if I said that some 'value 112 00:05:26,200 --> 00:05:30,700 = my_cats.append("Henry")' 113 00:05:30,700 --> 00:05:34,000 So we're going to put 'Henry' back in the list, right? Before 114 00:05:34,090 --> 00:05:37,440 we weren't doing 'value =', or variable is equal to, 115 00:05:37,450 --> 00:05:38,460 let's call it 'var'. 116 00:05:38,610 --> 00:05:39,810 'var = my_cats', 117 00:05:39,820 --> 00:05:40,620 we weren't doing that. 118 00:05:40,630 --> 00:05:43,670 We were just simply saying, 'my_cats.append', and then we put 119 00:05:43,670 --> 00:05:46,300 something in there, but this time we're not doing that. 120 00:05:46,380 --> 00:05:48,360 Okay, looks like everything's fine. 121 00:05:48,370 --> 00:05:51,690 And when we check out 'my_cats', 'my_cats' is going to be totally 122 00:05:51,700 --> 00:05:53,400 fine. It's going to do exactly what we said. 123 00:05:53,400 --> 00:05:54,800 It added 'Henry' in there. 124 00:05:54,800 --> 00:05:59,000 But what is 'var' going to be if 'my_cats' is already changed? 125 00:05:59,000 --> 00:06:01,800 Is this, whole this, now going to be in 'var'? 126 00:06:03,200 --> 00:06:04,200 Let's take a look at this. 127 00:06:05,800 --> 00:06:07,100 There's actually nothing in there. 128 00:06:07,800 --> 00:06:10,200 Let's take a look at the 'type(var)'. 129 00:06:11,100 --> 00:06:14,300 'None'. Now, that is another data type that we will eventually get 130 00:06:14,300 --> 00:06:17,000 into, but 'None' in Python means null. 131 00:06:17,000 --> 00:06:19,000 It's void. There's nothing in there. 132 00:06:19,260 --> 00:06:23,110 And so when we're running methods on a list like this, like 133 00:06:23,200 --> 00:06:27,400 '.append', or like, '.remove', we don't need to assign this variable. 134 00:06:27,400 --> 00:06:28,600 It's not useful. 135 00:06:28,640 --> 00:06:31,130 It's already going to update its internal workings of how 136 00:06:31,140 --> 00:06:34,950 this list works, and it's going to return absolutely nothing 137 00:06:34,950 --> 00:06:36,000 or a 'NoneType', 138 00:06:36,000 --> 00:06:39,240 and it's going to put that into the variable that we called 139 00:06:39,250 --> 00:06:40,320 'var'. So it's not useful. 140 00:06:40,440 --> 00:06:42,540 So you don't ever need to do that with a list. 141 00:06:42,550 --> 00:06:45,030 You can always just work directly on the list itself. 142 00:06:45,220 --> 00:06:47,650 Now, the reason I bring that up is because quite a while 143 00:06:47,660 --> 00:06:50,890 ago we said something like, 'name = "Kalob", and then 144 00:06:50,900 --> 00:06:54,000 we overwrote it with 'name = "Zephyr"'. 145 00:06:54,040 --> 00:06:56,650 Well, when it comes to list, we don't need to do that unless 146 00:06:56,660 --> 00:06:58,510 we want to replace the entire thing. 147 00:06:58,520 --> 00:07:03,450 So we could say 'name = [1, 2, 3]', and this 148 00:07:03,450 --> 00:07:07,100 is going to overwrite it with [4, 5, 6]. 149 00:07:07,100 --> 00:07:10,600 And then let's 'print(name)', and I'll move that up as well. 150 00:07:11,600 --> 00:07:16,000 We print the 'name'. It was assigned, but then was immediately 151 00:07:16,080 --> 00:07:18,740 reassigned. That's sort of what we did quite a while ago. 152 00:07:18,750 --> 00:07:21,290 And I'm just showing you that we don't need to do that when 153 00:07:21,300 --> 00:07:24,700 it comes to lists, unless you want to replace the entire thing. 154 00:07:24,740 --> 00:07:27,470 Now, one last thing I want to show you is just to make this 155 00:07:27,480 --> 00:07:28,550 a little more confusing, 156 00:07:28,560 --> 00:07:31,370 because I'm sure this is totally not super confusing right now, 157 00:07:31,370 --> 00:07:35,300 but it does get easier over time, is if we look at 158 00:07:35,300 --> 00:07:36,500 'my_cats' once more, 159 00:07:36,780 --> 00:07:40,860 we have '['Henry', 'Ezra', 'Zephyr']', not in that order. 160 00:07:40,860 --> 00:07:45,150 We have '['Zephyr', 'Ezra', 'Henry']', and let's say I just wanted to 161 00:07:45,150 --> 00:07:46,800 get just the last item. 162 00:07:46,800 --> 00:07:49,500 Now, when you're just learning to program, you're probably 163 00:07:49,500 --> 00:07:50,900 thinking, "Well, that's not going to be useful. 164 00:07:50,900 --> 00:07:53,100 Why would I ever want just the last item?" 165 00:07:53,900 --> 00:07:56,900 But there are going to be a lot of cases down the road where 166 00:07:56,910 --> 00:07:58,220 you do just want the last item. 167 00:07:58,230 --> 00:08:02,270 So what we can do is a thing called 'pop', and we can say 'henry 168 00:08:02,270 --> 00:08:05,700 = my_cats.pop()'. 169 00:08:05,780 --> 00:08:08,270 Now, this method is interesting. 170 00:08:08,280 --> 00:08:12,020 It's going to take this value, and is going to pull it out 171 00:08:12,030 --> 00:08:13,520 of that list, update the list, 172 00:08:13,530 --> 00:08:17,780 so it's just ['Zephyr', 'Ezra']', and is going to put 'Henry' into 173 00:08:17,790 --> 00:08:20,030 its own variable, which we happen to also call 'henry'. 174 00:08:20,100 --> 00:08:23,500 So let's go ahead and run that and let's see what 'henry' is, 175 00:08:23,540 --> 00:08:28,450 and let's see what 'my_cats'. Look at that. 176 00:08:28,460 --> 00:08:30,100 So it actually took it out. 177 00:08:30,110 --> 00:08:32,770 This is one of those times where you don't just want to use 178 00:08:33,000 --> 00:08:34,500 this on itself, 179 00:08:34,500 --> 00:08:36,299 otherwise it's not going to do anything. 180 00:08:36,299 --> 00:08:37,700 It's just going to get rid of that last one. 181 00:08:37,880 --> 00:08:42,230 Now, as an example, if we don't reassign this to a variable 182 00:08:42,230 --> 00:08:46,799 or assign this to a variable, 'my_cats' is going to be '['Zephyr', 'Ezra']'. 183 00:08:47,600 --> 00:08:53,000 But if I run 'pop' on this, again, it's going to take this last 184 00:08:53,090 --> 00:08:55,909 item, and it's going to try to reassign it to a variable, 185 00:08:55,919 --> 00:08:57,260 but there's no variable this time. 186 00:08:57,420 --> 00:09:01,930 So all it's going to do is get rid of this last item, which 187 00:09:01,930 --> 00:09:06,300 is the exact same as saying 'remove("Ezra")'. 188 00:09:06,350 --> 00:09:07,970 But we don't want to do that this time. 189 00:09:07,980 --> 00:09:09,050 We just want to 'pop' it. 190 00:09:09,060 --> 00:09:10,370 Just get rid of that last one. 191 00:09:10,400 --> 00:09:13,040 You can see it returns something called 'Ezra'. 192 00:09:13,250 --> 00:09:16,230 But 'my_cats' is now just 'Zephyr'. 193 00:09:16,240 --> 00:09:18,840 And this little piece of data was lost forever. 194 00:09:18,840 --> 00:09:21,300 It was not stored in a variable of any kind, 195 00:09:21,310 --> 00:09:22,470 it wasn't put anywhere else, 196 00:09:22,600 --> 00:09:27,300 it just simply returned the item called 'Ezra'. 197 00:09:27,300 --> 00:09:30,170 And Python said, "Okay. Well, you obviously don't want it. 198 00:09:30,180 --> 00:09:33,410 Get rid of it", and updated our list with just 'Zephyr'. 199 00:09:33,560 --> 00:09:36,350 Now, what I would like you to do for your fun little task 200 00:09:36,350 --> 00:09:39,040 here, is make a list. 201 00:09:39,200 --> 00:09:43,600 Your cats, your friends, your favorite hobbies, whatever it is, 202 00:09:43,650 --> 00:09:47,240 and then I want you to, where is it append to it. 203 00:09:47,250 --> 00:09:49,970 So add an item, and then I want you to remove one of those 204 00:09:49,980 --> 00:09:53,770 items, and then I would also like you to 'pop' that item, or 205 00:09:53,780 --> 00:09:56,500 'pop' the last item into its own variable. 206 00:09:56,510 --> 00:09:59,440 Print all of that stuff out into your Jupyter Notebook or your 207 00:09:59,700 --> 00:10:01,590 Python interactive shell. 208 00:10:01,600 --> 00:10:05,500 And once you think you sort of have that figured out, let's 209 00:10:05,500 --> 00:10:07,100 move on to that next lesson.