WEBVTT 1 00:00:01.890 --> 00:00:03.610 line:15% Hi guys and welcome back. 2 00:00:03.610 --> 00:00:06.470 line:15% In this video, we're going to talk about lists. 3 00:00:06.470 --> 00:00:09.270 Lists allow us to store multiple values 4 00:00:09.270 --> 00:00:10.983 in a single variable. 5 00:00:11.830 --> 00:00:13.510 For example, imagine you've got a programme 6 00:00:13.510 --> 00:00:15.950 that contains the names of your friends, 7 00:00:15.950 --> 00:00:18.200 you could start off with something like this. 8 00:00:19.500 --> 00:00:22.710 But of course, you can imagine that as your programme grows, 9 00:00:22.710 --> 00:00:25.470 this becomes less and less feasible, 10 00:00:25.470 --> 00:00:28.870 you have to keep adding variables and not only that, 11 00:00:28.870 --> 00:00:30.830 but working with these variables 12 00:00:30.830 --> 00:00:32.610 becomes increasingly difficult. 13 00:00:32.610 --> 00:00:34.830 For example, if you wanted to print out 14 00:00:34.830 --> 00:00:37.410 who your friends are, you would have to say, 15 00:00:37.410 --> 00:00:39.930 you know, print(friend1) 16 00:00:39.930 --> 00:00:42.750 and then you have to print (friend2) and then three. 17 00:00:42.750 --> 00:00:44.360 And if you add more, 18 00:00:44.360 --> 00:00:47.110 you would have to add more print statements. 19 00:00:47.110 --> 00:00:50.350 And that means that as your code grows, 20 00:00:50.350 --> 00:00:53.450 it grows more and more and more exponentially. 21 00:00:53.450 --> 00:00:55.150 And that means that this code is not going 22 00:00:55.150 --> 00:00:57.600 to be very easy to work with. 23 00:00:57.600 --> 00:01:00.803 It's much better, if we create a list. 24 00:01:02.061 --> 00:01:05.150 (keyboard typing) 25 00:01:05.150 --> 00:01:06.130 Like this. 26 00:01:06.130 --> 00:01:08.930 Now we've got a single variable, friends 27 00:01:08.930 --> 00:01:12.550 and that name describes very well what it contains, 28 00:01:12.550 --> 00:01:15.260 which is the names of our friends. 29 00:01:15.260 --> 00:01:18.300 To create a list, we use square brackets 30 00:01:18.300 --> 00:01:22.370 and inside the list, we put the different values 31 00:01:22.370 --> 00:01:24.360 separated by commas. 32 00:01:24.360 --> 00:01:28.350 So what we've got here, is a list of three strings. 33 00:01:28.350 --> 00:01:31.010 The first one is Rolf, the second one is Bob 34 00:01:31.010 --> 00:01:32.110 and the third is Anne. 35 00:01:33.240 --> 00:01:36.210 If we want to access the value of a single friend, 36 00:01:36.210 --> 00:01:39.540 you would do friends and then using square brackets again, 37 00:01:39.540 --> 00:01:41.770 you would put zero for example. 38 00:01:41.770 --> 00:01:42.603 And what this does, 39 00:01:42.603 --> 00:01:45.713 is it accesses the first element of the list. 40 00:01:46.840 --> 00:01:50.070 Notice that in computing, we start counting at zero 41 00:01:50.070 --> 00:01:52.520 so element zero is the first element, 42 00:01:52.520 --> 00:01:54.950 element one is the second element and so on. 43 00:01:54.950 --> 00:01:58.160 So if we run this, you'll see that Rolf gets printed out 44 00:01:58.160 --> 00:02:00.790 because that's the first element there. 45 00:02:00.790 --> 00:02:02.470 Similarly, if we print friends[1], 46 00:02:02.470 --> 00:02:04.840 you'll see Bob printed out. 47 00:02:04.840 --> 00:02:07.970 Notice that you can put anything you want inside a list, 48 00:02:07.970 --> 00:02:10.930 so you could put two in there if you wanted to 49 00:02:10.930 --> 00:02:12.770 but this is highly discouraged. 50 00:02:12.770 --> 00:02:17.770 Generally, you want to keep data inside a list, homogeneous. 51 00:02:18.010 --> 00:02:20.700 And what this means is, you wanna keep it all related 52 00:02:20.700 --> 00:02:21.960 and all the same. 53 00:02:21.960 --> 00:02:25.000 For example, here if you've got a list called friends, 54 00:02:25.000 --> 00:02:27.680 you probably wanna keep your friends in that list. 55 00:02:27.680 --> 00:02:29.790 If you have a list called furniture, 56 00:02:29.790 --> 00:02:32.500 you would wanna keep strings describing the furniture 57 00:02:32.500 --> 00:02:34.160 in that list. 58 00:02:34.160 --> 00:02:36.570 If you see something like this out there in some code 59 00:02:36.570 --> 00:02:37.830 that you read somewhere else, 60 00:02:37.830 --> 00:02:40.890 it probably means that the programme in question, 61 00:02:40.890 --> 00:02:44.240 started writing some code about furniture 62 00:02:44.240 --> 00:02:47.420 and then decided to write about friends instead. 63 00:02:47.420 --> 00:02:49.100 And generally, when you do this, 64 00:02:49.100 --> 00:02:52.273 you want to make sure that your variables do change. 65 00:02:53.540 --> 00:02:55.960 While you can do this, is generally discouraged, 66 00:02:55.960 --> 00:03:00.410 because the data inside the list is no longer described 67 00:03:00.410 --> 00:03:04.000 by the variable name and so this can be a bit confusing. 68 00:03:04.000 --> 00:03:05.950 If you see a variable called friends, 69 00:03:05.950 --> 00:03:06.920 what's the two? 70 00:03:06.920 --> 00:03:08.600 Does that mean you have two friends? 71 00:03:08.600 --> 00:03:09.870 Is it something else? 72 00:03:09.870 --> 00:03:12.150 Maybe one of your friends is called two. 73 00:03:12.150 --> 00:03:15.990 So, keep the data inside your list's homogeneous 74 00:03:15.990 --> 00:03:17.690 and that's gonna make your life much easier 75 00:03:17.690 --> 00:03:19.530 as you programme more. 76 00:03:19.530 --> 00:03:22.650 Of course if you did want to get the amount of elements 77 00:03:22.650 --> 00:03:26.680 in the list, say two, you don't have to put the two 78 00:03:26.680 --> 00:03:27.740 inside the list. 79 00:03:27.740 --> 00:03:31.680 You can just print the len of friends. 80 00:03:31.680 --> 00:03:34.860 So if you do len, and then inside the brackets, 81 00:03:34.860 --> 00:03:38.010 you put the list, this is gonna give you the length 82 00:03:38.010 --> 00:03:39.340 of the list or the size of it. 83 00:03:39.340 --> 00:03:41.660 So if we run this, you'll get two out 84 00:03:42.630 --> 00:03:44.730 because that's the number of elements in this list, 85 00:03:44.730 --> 00:03:46.700 Rolf and Anne. 86 00:03:46.700 --> 00:03:49.290 You can put anything you want inside a list, 87 00:03:49.290 --> 00:03:51.630 so it doesn't have to be strings or numbers, 88 00:03:51.630 --> 00:03:53.280 you can put other lists if you want. 89 00:03:53.280 --> 00:03:57.903 For example, let's create a list of friends and their ages. 90 00:03:59.630 --> 00:04:00.750 So here, this outer list, 91 00:04:00.750 --> 00:04:03.550 denoted by these square brackets outside 92 00:04:03.550 --> 00:04:07.420 of every other element, has three lists inside it. 93 00:04:07.420 --> 00:04:11.143 This one, that one and this one. 94 00:04:12.230 --> 00:04:14.943 Each of those lists has two elements inside it. 95 00:04:16.300 --> 00:04:19.870 Those two in this list, Bob and 30 in this list 96 00:04:19.870 --> 00:04:22.593 and Anne and 27 in the final list. 97 00:04:23.620 --> 00:04:25.730 If you wanted to access a specific element, 98 00:04:25.730 --> 00:04:28.230 for example, Rolf, all you have to do 99 00:04:28.230 --> 00:04:33.230 is access the first element of the outer list 100 00:04:33.810 --> 00:04:36.620 and that is friends[0] 101 00:04:36.620 --> 00:04:40.430 and then the first element of the inner list. 102 00:04:40.430 --> 00:04:43.800 So when you do friends[0], what you're getting back 103 00:04:43.800 --> 00:04:45.310 is this list here. 104 00:04:45.310 --> 00:04:47.720 What happens in Python, is that this is replaced 105 00:04:47.720 --> 00:04:49.950 essentially by this list. 106 00:04:49.950 --> 00:04:53.310 So now if you wanna access the first element of this list, 107 00:04:53.310 --> 00:04:57.490 you just put another zero inside square brackets at the end. 108 00:04:57.490 --> 00:05:02.410 So this here is this list and all of this is Rolf. 109 00:05:04.340 --> 00:05:06.650 Similarly, if you wanted to access Bob's age, 110 00:05:06.650 --> 00:05:09.977 for example, you could do friends[1][1]. 111 00:05:11.200 --> 00:05:13.410 When you have a long list, such as this one, 112 00:05:13.410 --> 00:05:16.410 is usually good form to split it out into multiple lines 113 00:05:16.410 --> 00:05:19.820 like this, the square brackets in their own lines. 114 00:05:19.820 --> 00:05:22.650 And then, with some spacing in front, 115 00:05:22.650 --> 00:05:26.180 although it is not required, but it is generally used, 116 00:05:26.180 --> 00:05:29.230 you can put each of the sub lists in there. 117 00:05:29.230 --> 00:05:32.010 Indeed if it's a list of strings, say, 118 00:05:32.010 --> 00:05:34.390 you can also put each string in their own line 119 00:05:34.390 --> 00:05:36.050 just so it's a bit easier to read. 120 00:05:36.050 --> 00:05:38.410 This clearly shows that you've got a long list 121 00:05:38.410 --> 00:05:40.800 and this is each element in the list 122 00:05:40.800 --> 00:05:43.200 and you can see that comma separated at the end. 123 00:05:44.480 --> 00:05:47.330 Now pay close attention when you are writing lists 124 00:05:47.330 --> 00:05:50.370 that you have the commas in the right place. 125 00:05:50.370 --> 00:05:52.980 Here, we've got a comma inside this inner list, 126 00:05:52.980 --> 00:05:54.700 which means its got two elements. 127 00:05:54.700 --> 00:05:56.250 And then we've got a comment afterwards, 128 00:05:56.250 --> 00:05:59.350 which separates this element from other elements 129 00:05:59.350 --> 00:06:03.140 in the outer list, just something to pay close attention to. 130 00:06:03.140 --> 00:06:06.480 Let's go back to our friends list with just strings 131 00:06:06.480 --> 00:06:08.970 and look at how to add to one of these. 132 00:06:08.970 --> 00:06:10.840 To add to a list, all you have to do is 133 00:06:10.840 --> 00:06:15.280 type the list name dot append and what that's gonna do 134 00:06:15.280 --> 00:06:18.850 is going to add a name or indeed add anything 135 00:06:18.850 --> 00:06:20.670 to the list at the end. 136 00:06:20.670 --> 00:06:22.550 That's what append means. 137 00:06:22.550 --> 00:06:24.490 So if we print friends now, 138 00:06:24.490 --> 00:06:26.360 you'll see that we've got four elements 139 00:06:26.360 --> 00:06:28.363 inside the friends list. 140 00:06:30.530 --> 00:06:33.100 Similarly you can remove from a list if you want 141 00:06:33.100 --> 00:06:36.440 by doing friends.remove and then say Bob. 142 00:06:36.440 --> 00:06:37.520 Now if we print this, you'll see that 143 00:06:37.520 --> 00:06:41.230 there is only got two elements, Rolf and Anne. 144 00:06:41.230 --> 00:06:42.170 Something to remember is that, 145 00:06:42.170 --> 00:06:44.820 if you have a list of lists, for example 146 00:06:44.820 --> 00:06:47.190 and you want to remove one of the sub lists, 147 00:06:47.190 --> 00:06:49.740 you've gonna say friends.remove 148 00:06:49.740 --> 00:06:51.610 and then everything you want to remove 149 00:06:51.610 --> 00:06:54.030 so that's Anne, say 27. 150 00:06:54.030 --> 00:06:56.610 And what this means is, you're going to remove 151 00:06:56.610 --> 00:06:59.540 from the friends list, which is all of this, 152 00:06:59.540 --> 00:07:02.150 the value of this list, 153 00:07:02.150 --> 00:07:04.630 so this entire list is gonna go away. 154 00:07:04.630 --> 00:07:08.190 If you just do Anne, Python is gonna say, 155 00:07:08.190 --> 00:07:11.390 Anne doesn't exist in this list, because this list contains 156 00:07:11.390 --> 00:07:16.090 three things, this, that and that 157 00:07:16.090 --> 00:07:18.660 and Anne is not one of those things. 158 00:07:18.660 --> 00:07:21.570 What is one of those things, though, is this entire list, 159 00:07:21.570 --> 00:07:23.323 and with 27 in it. 160 00:07:24.420 --> 00:07:26.480 So, if we print friends now, 161 00:07:26.480 --> 00:07:28.790 you'll see that entirely disappears 162 00:07:28.790 --> 00:07:31.470 and you're left with just the two other lists 163 00:07:31.470 --> 00:07:33.530 inside your friends list. 164 00:07:33.530 --> 00:07:34.840 Hopefully this makes sense. 165 00:07:34.840 --> 00:07:38.840 Lists are a very useful data structure 166 00:07:38.840 --> 00:07:40.450 and they're everywhere in Python 167 00:07:40.450 --> 00:07:42.160 and you're gonna be using them a lot 168 00:07:42.160 --> 00:07:43.900 throughout your programming. 169 00:07:43.900 --> 00:07:46.110 If you're not sure when you would want to use a list 170 00:07:46.110 --> 00:07:48.580 versus a string, don't worry too much about it, 171 00:07:48.580 --> 00:07:50.480 over the coming videos, we're going to be getting 172 00:07:50.480 --> 00:07:53.120 more experience with using lists 173 00:07:53.120 --> 00:07:54.760 as well as some other data structures 174 00:07:54.760 --> 00:07:57.550 that will be looking at and you'll learn more about when 175 00:07:57.550 --> 00:07:59.460 and how to use them. 176 00:07:59.460 --> 00:08:01.953 So with that said, I'll see you in the next video.