WEBVTT 1 00:00:01.859 --> 00:00:05.760 In this video we're going to go through an introduction to lists. 2 00:00:05.760 --> 00:00:09.120 You've already looked at one sequence type, which is the tech sequence type, 3 00:00:09.120 --> 00:00:10.600 STR or string, and 4 00:00:10.600 --> 00:00:13.450 we also looked at some of the operations that can be performed on the string. 5 00:00:13.450 --> 00:00:17.280 Now Python actually provides six additional built in sequence types. 6 00:00:17.280 --> 00:00:20.360 And they are the List, the Range, the Tuple and 7 00:00:20.360 --> 00:00:22.180 these three types we'll be discussing in this section. 8 00:00:22.180 --> 00:00:25.780 And there's also three binary sequence types that will be the subject of future 9 00:00:25.780 --> 00:00:27.080 sections, but we'll talk about those. 10 00:00:27.080 --> 00:00:30.240 So there's actually useful summary of the operations that can be performed on 11 00:00:30.240 --> 00:00:31.300 sequences. 12 00:00:31.300 --> 00:00:34.540 What I'm going to do is just open up some documentation and show you that. 13 00:00:35.660 --> 00:00:40.210 Gonna paste the link there and you can see the link now just by making it a little 14 00:00:40.210 --> 00:00:42.310 bit larger so you can read it on the screen. 15 00:00:42.310 --> 00:00:45.540 If you go to that page and have a look, you can see that there's a good summary as 16 00:00:45.540 --> 00:00:48.580 I mentioned of the various operations that can be performed. 17 00:00:48.580 --> 00:00:51.640 And the table itself is actually quite useful, but 18 00:00:51.640 --> 00:00:55.370 unfortunately the actual text itself is written as a formal definition. 19 00:00:55.370 --> 00:00:58.930 And it's really not that easy to understand. 20 00:00:58.930 --> 00:01:02.200 Good news is that we'll be explaining it all in this course here anyway. 21 00:01:02.200 --> 00:01:03.960 You're looking at these operations on the screen. 22 00:01:03.960 --> 00:01:07.730 Even though they're a little bit hard, bit cryptic to read what they actually mean, 23 00:01:07.730 --> 00:01:11.120 we've actually used the first eight already in the course. 24 00:01:11.120 --> 00:01:12.794 And if you look at the next two, min and max, 25 00:01:12.794 --> 00:01:14.840 they're probably fairly obvious as to what they do. 26 00:01:14.840 --> 00:01:17.540 Give you the smallest item and the largest item. 27 00:01:17.540 --> 00:01:20.500 And even if the results are reduced, they are not always what you might expect. 28 00:01:20.500 --> 00:01:22.310 Which you'll actually find we're not actually using them. 29 00:01:22.310 --> 00:01:25.060 But count could've been useful in our previous change as well. 30 00:01:25.060 --> 00:01:28.580 This is the s.count, total number of occurrences, and we could've actually used 31 00:01:28.580 --> 00:01:32.150 that to check that the input string contained exactly three full stops. 32 00:01:32.150 --> 00:01:35.990 So let's actually go back and check that out and see how to do that in some code. 33 00:01:35.990 --> 00:01:41.494 So, what we could've done is started off and put ipAddress = input. 34 00:01:44.183 --> 00:01:49.405 Please enter an IP address, like so. 35 00:01:49.405 --> 00:01:51.637 In terms of validating it, 36 00:01:51.637 --> 00:01:56.885 we could've done print ipaddress.count and put dot in there. 37 00:01:56.885 --> 00:01:59.885 So we're counting how many dots, full stops are actually in there. 38 00:01:59.885 --> 00:02:00.825 So if we actually run that. 39 00:02:02.067 --> 00:02:06.172 Please enter an IP address 192.168.0.1. 40 00:02:06.172 --> 00:02:07.777 You can see we got the number three there. 41 00:02:07.777 --> 00:02:10.585 And again we can just try another one. 42 00:02:10.585 --> 00:02:14.200 We could just try a bogus one 10.5.4.3.2.1. 43 00:02:14.200 --> 00:02:17.140 And that's returning five even, that's an invalid IP address. 44 00:02:17.140 --> 00:02:20.040 You can see that it's actually correctly returning the right number 45 00:02:20.040 --> 00:02:21.110 from stops it's found. 46 00:02:21.110 --> 00:02:22.490 So that would have been very useful. 47 00:02:22.490 --> 00:02:24.790 There's also another I want that we're gonna look at in this section. 48 00:02:24.790 --> 00:02:28.880 If we go back to the form again, we're gonna be using index. 49 00:02:28.880 --> 00:02:31.280 Index or the first occurrence of x in s. 50 00:02:31.280 --> 00:02:35.270 So all these operations that I'm showing you can be performed on any sequence type, 51 00:02:35.270 --> 00:02:37.380 but we're actually gonna be using them with lists here. 52 00:02:37.380 --> 00:02:38.420 Let's go back to the code again. 53 00:02:38.420 --> 00:02:41.940 It probably helps though if we start off by talking about what is a list. 54 00:02:41.940 --> 00:02:43.770 So what actually is a list in Python? 55 00:02:43.770 --> 00:02:46.190 Think of a list as a sequence of things. 56 00:02:46.190 --> 00:02:48.910 Now those things could be strings, numbers, classes, or 57 00:02:48.910 --> 00:02:50.600 pretty much anything else. 58 00:02:50.600 --> 00:02:53.120 So if a list can be a sequence of strings, and 59 00:02:53.120 --> 00:02:56.600 a string is itself a sequence type which we talked about earlier, 60 00:02:56.600 --> 00:03:00.110 then it makes sense that a list can also be a sequence of lists, which it can. 61 00:03:00.110 --> 00:03:01.570 So this makes an incredibly useful, 62 00:03:01.570 --> 00:03:03.780 if a little bit confusing initially, as we're about to see. 63 00:03:03.780 --> 00:03:07.300 So we're gonna actually start by looking at the dead parrot list we used when 64 00:03:07.300 --> 00:03:09.150 discussing four loops in a previous section. 65 00:03:09.150 --> 00:03:11.960 We're gonna make a slight change and assign the list to a variable So 66 00:03:11.960 --> 00:03:13.700 I'm just going to comment this code out. 67 00:03:15.130 --> 00:03:20.104 So let's type parent_list = 68 00:03:20.104 --> 00:03:25.840 ["non pinin'", "no more", 69 00:03:25.840 --> 00:03:31.210 "a stiff", bereft of love". 70 00:03:32.630 --> 00:03:34.730 Again, Monty Python references there. 71 00:03:34.730 --> 00:03:37.600 And this time I've assigned it, as you can see, to a variable. 72 00:03:37.600 --> 00:03:45.260 And we can use our for loop, for state in parrot list: and print. 73 00:03:45.260 --> 00:03:49.965 This parrot is + state. 74 00:03:49.965 --> 00:03:52.775 So again, the list on line three it's 75 00:03:52.775 --> 00:03:56.285 created when closing the individual items in square brackets as you can see there. 76 00:03:56.285 --> 00:03:59.520 It's separating each item in the list with a comma. 77 00:03:59.520 --> 00:04:02.090 And we can actually run this to confirm that it works. 78 00:04:02.090 --> 00:04:04.780 As parrot is, you can see non-pointing is the first entry, 79 00:04:04.780 --> 00:04:07.890 no more was the second one, stiff and bereft of love. 80 00:04:07.890 --> 00:04:13.400 So it actually went through the flow each entry is a separate entry in the list and 81 00:04:13.400 --> 00:04:14.920 we're able to print a message out. 82 00:04:14.920 --> 00:04:18.990 It's also quite easy to append to a list after you've created it. 83 00:04:18.990 --> 00:04:22.080 So if you wanted to do that elsewhere in the code, you could actually come down in 84 00:04:22.080 --> 00:04:28.580 here and type parrot_list.append and type in something like Norwegian Blue. 85 00:04:28.580 --> 00:04:34.060 And then if we run that, we can 86 00:04:34.060 --> 00:04:38.760 see that we successfully added an entry to the end of the list by using the .append. 87 00:04:38.760 --> 00:04:40.660 Now the other thing we can do is have a list of numbers. 88 00:04:40.660 --> 00:04:42.390 So, obviously we've used strings there. 89 00:04:42.390 --> 00:04:44.880 But there's nothing stopping us from actually having numbers so 90 00:04:44.880 --> 00:04:48.540 we can do something like, even equals 2, 4, 6, 8. 91 00:04:48.540 --> 00:04:55.890 And we can say odd equals 1, 3, 5, 7, 9, like so. 92 00:04:57.180 --> 00:05:05.550 And we could put numbers = even + odd, print numbers. 93 00:05:05.550 --> 00:05:08.310 So here we are actually concatenating the even and odd lists and 94 00:05:08.310 --> 00:05:12.060 we're assigning the result to a third list, a new list, called numbers. 95 00:05:12.060 --> 00:05:14.400 So let's actually try running that first. 96 00:05:14.400 --> 00:05:19.300 And you notice we've actually got 246813579 so it successfully appended it 97 00:05:19.300 --> 00:05:23.740 to numbers that were in the odd list at the end of the even list and 98 00:05:23.740 --> 00:05:26.750 actually created a third list when we actually added to the numbers. 99 00:05:26.750 --> 00:05:30.065 So the actual numbers list contains the sum of both of them. 100 00:05:30.065 --> 00:05:31.490 And we've got a couple of errors so 101 00:05:31.490 --> 00:05:34.160 I should actually make an attempt to actually fix those. 102 00:05:34.160 --> 00:05:37.590 So we need to be adding that as white space we have to get in to do this for 103 00:05:37.590 --> 00:05:41.430 all our code, to make sure the code's actually valid each time. 104 00:05:41.430 --> 00:05:44.580 And this is actually telling us that the list creation could be rewritten as 105 00:05:44.580 --> 00:05:45.640 a list literal. 106 00:05:45.640 --> 00:05:47.460 So I think and that's some more advice from IntelliJ, but 107 00:05:47.460 --> 00:05:49.160 we'll actually leave that for the time being anyway. 108 00:05:49.160 --> 00:05:50.230 We'll come back to that. 109 00:05:50.230 --> 00:05:53.070 But looking at the list that's been combined there, 110 00:05:53.070 --> 00:05:55.210 as you can see, they're not actually in order. 111 00:05:55.210 --> 00:05:58.020 So obviously it'd be nice if we could automatically get those in order so 112 00:05:58.020 --> 00:06:00.380 they actually started from lowest number to highest number. 113 00:06:00.380 --> 00:06:01.890 And that's actually quite easy to do. 114 00:06:01.890 --> 00:06:06.010 So we can actually, very simply use the sort method which is part of a list and 115 00:06:06.010 --> 00:06:09.330 type numbers.sort, like so. 116 00:06:09.330 --> 00:06:10.620 And once we do that and 117 00:06:10.620 --> 00:06:14.680 run it again you can see we've now got the numbers correctly in order. 118 00:06:14.680 --> 00:06:18.860 Now the sort method itself doesn't return the sorted list, and 119 00:06:18.860 --> 00:06:21.800 if you've actually used other languages this may be what you expect. 120 00:06:21.800 --> 00:06:24.990 You may expect the sort method to actually return the sorted list, so 121 00:06:24.990 --> 00:06:26.490 in other words this is not going to work. 122 00:06:26.490 --> 00:06:29.270 So, if I give you an example, if we actually remove that, 123 00:06:29.270 --> 00:06:31.330 and actually comment that line out. 124 00:06:32.330 --> 00:06:36.410 And if you did something like this, print numbers.sort, you might assume that that 125 00:06:36.410 --> 00:06:39.960 would actually print out the contents of what was a sort of numbers. 126 00:06:39.960 --> 00:06:42.296 And certainly that would be the case in other programming language. 127 00:06:42.296 --> 00:06:45.930 But if you run that we actually get nine, we get something completely different. 128 00:06:45.930 --> 00:06:48.870 And the reason for that is that Guido Van Rossum, 129 00:06:48.870 --> 00:06:52.800 the guy who invented Python, he chose his behavior deliberately, and 130 00:06:52.800 --> 00:06:55.730 the idea was to provide a hint that the sort method works on the object that it 131 00:06:55.730 --> 00:06:59.150 was called upon, rather than creating a new object. 132 00:06:59.150 --> 00:07:02.560 And that's very important here that It's not actually creating anything. 133 00:07:02.560 --> 00:07:03.990 So the sort method or 134 00:07:03.990 --> 00:07:08.390 function, the idea of it is that it actually works on the existing variable, 135 00:07:08.390 --> 00:07:11.630 the object in other words, it doesn't actually create a new object. 136 00:07:11.630 --> 00:07:15.130 And that's why in this case .sort return nothing. 137 00:07:15.130 --> 00:07:18.800 So, we would then delete that code out again and come back here. 138 00:07:18.800 --> 00:07:21.445 And so, we've got at this point, our list. 139 00:07:21.445 --> 00:07:25.300 Numbers.sort works on that list, and resorts out, and then printing it out 140 00:07:25.300 --> 00:07:28.750 obviously prints out the same variable, which has now been sorted. 141 00:07:28.750 --> 00:07:31.180 And I've got the numbers in the right order like so. 142 00:07:31.180 --> 00:07:35.010 So if you are new to programming, some of this discussion may not make much sense. 143 00:07:35.010 --> 00:07:38.190 You might be wondering what I'm talking about when I'm talking about the .sort 144 00:07:38.190 --> 00:07:39.350 returning an object. 145 00:07:39.350 --> 00:07:41.160 And you are probably quite happy with the way it works. 146 00:07:41.160 --> 00:07:43.880 And I think indeed it actually does make sense and it's quite intuitive. 147 00:07:43.880 --> 00:07:47.040 But the reason I'm bringing it up is because it's particularly important if you 148 00:07:47.040 --> 00:07:50.440 have a seen this in other languages you have to get used to this type of 149 00:07:50.440 --> 00:07:51.589 behavior [INAUDIBLE]. 150 00:07:51.589 --> 00:07:56.503 Generally speaking if a method acts on an object and changes it, 151 00:07:56.503 --> 00:07:58.600 mutates it in other words. 152 00:07:58.600 --> 00:08:01.660 Then the method is going to return none but 153 00:08:01.660 --> 00:08:04.100 the actual variable object has been updated. 154 00:08:04.100 --> 00:08:07.450 Now there is a way around this if you want to create a new object rather than 155 00:08:07.450 --> 00:08:08.260 sorting the one you have, 156 00:08:08.260 --> 00:08:11.620 there's actually a built in function sorter that's going to do that. 157 00:08:11.620 --> 00:08:13.600 So we could actually change this code out. 158 00:08:13.600 --> 00:08:18.044 So we'll get rid of .sort and what we'll do is actually come up here and 159 00:08:18.044 --> 00:08:22.060 delete that code and put numbers but use a method called sorted. 160 00:08:24.404 --> 00:08:29.220 Like so and sorted is a function that's brought into Python. 161 00:08:29.220 --> 00:08:31.850 If we run that, you can see we've got the same result, 162 00:08:31.850 --> 00:08:35.010 we've actually got it sorted, and that does actually return and the number, 163 00:08:35.010 --> 00:08:36.360 cuz obviously we're printing this out. 164 00:08:36.360 --> 00:08:40.250 So it's returned a new list containing the sorted numbers. 165 00:08:40.250 --> 00:08:43.530 So that's different than the .sort function you saw before. 166 00:08:43.530 --> 00:08:46.930 This sorted function actually returns the actual new object. 167 00:08:46.930 --> 00:08:49.220 And of course, with that said, we're printing it there, but 168 00:08:49.220 --> 00:08:52.873 there's nothing stopping us actually just putting 169 00:08:52.873 --> 00:08:57.420 numbers_in_order = sorted(numbers), like so. 170 00:08:57.420 --> 00:08:58.380 And then just printing. 171 00:08:59.420 --> 00:09:03.160 Print numbers in order, which essentially is the same thing, so 172 00:09:03.160 --> 00:09:04.210 that's going to work as well. 173 00:09:04.210 --> 00:09:07.470 If we run that, we get the numbers at sorted in the right order. 174 00:09:07.470 --> 00:09:09.140 So, at that point, obviously, we've got two lists. 175 00:09:09.140 --> 00:09:12.860 We've got numbers, and numbers in order, and they've got the same items, but, 176 00:09:12.860 --> 00:09:13.850 obviously, different orders. 177 00:09:13.850 --> 00:09:16.870 The first one, numbers has got the numbers unsorted, and 178 00:09:16.870 --> 00:09:19.590 numbers in order has obviously has got the sorted numbers. 179 00:09:19.590 --> 00:09:21.450 The other thing to keep in mind is at this point, 180 00:09:21.450 --> 00:09:25.280 even though they can both contain the same items, Python will 181 00:09:25.280 --> 00:09:28.360 not treat them as equal as we can see with this code we're about to write. 182 00:09:28.360 --> 00:09:33.341 And this important distinction is, 183 00:09:33.341 --> 00:09:38.322 over top, if numbers equals numbers 184 00:09:38.322 --> 00:09:43.150 in order Print the lists are equal. 185 00:09:43.150 --> 00:09:47.080 And again they've got the same numbers in them so you'd think maybe that would work, 186 00:09:47.080 --> 00:09:48.580 but we'll try putting an else in there. 187 00:09:49.930 --> 00:09:51.790 The lists are not equal. 188 00:09:54.160 --> 00:09:55.150 So, I've actually run that. 189 00:09:56.500 --> 00:09:59.070 We actually get the message back, the lists are not equal. 190 00:09:59.070 --> 00:10:01.840 And that's because even though we've got the same numbers, 191 00:10:01.840 --> 00:10:03.620 they're actually in a different order. 192 00:10:03.620 --> 00:10:06.880 So obviously the first set of numbers was unordered, and 193 00:10:06.880 --> 00:10:08.860 the second set of numbers was ordered. 194 00:10:08.860 --> 00:10:11.510 And as far as parsing is concerned, they aren't equal. 195 00:10:11.510 --> 00:10:13.150 But we could do something like this. 196 00:10:13.150 --> 00:10:18.870 We could actually do a test for if numbers_in_order 197 00:10:18.870 --> 00:10:21.930 is equal to sorted numbers. 198 00:10:23.770 --> 00:10:25.760 Print, and we'll actually copy the rest of the code there, so 199 00:10:25.760 --> 00:10:27.580 we'll just do exactly the same. 200 00:10:27.580 --> 00:10:29.770 Copy that, paste that. 201 00:10:29.770 --> 00:10:33.070 So now we're actually testing numbers in an order which has already been sorted, 202 00:10:33.070 --> 00:10:36.190 and we're comparing that to the return from this sorted function, 203 00:10:36.190 --> 00:10:40.410 which will actually be numbers, sorting the unsorted list or sorted lists, 204 00:10:40.410 --> 00:10:41.860 that should now come back and say they're equal. 205 00:10:41.860 --> 00:10:43.710 And the lists are equal at this point, 206 00:10:43.710 --> 00:10:47.990 because obviously we've got same numbers and also they're both sorted as well. 207 00:10:47.990 --> 00:10:50.470 So again, if you're used to other programming languages, 208 00:10:50.470 --> 00:10:52.590 this may well be very confusing to you. 209 00:10:52.590 --> 00:10:56.540 The two objects are not the same and they would not test equal in Java for example. 210 00:10:56.540 --> 00:11:00.090 But Python works as you would intuitively would expect if you're new to programming. 211 00:11:00.090 --> 00:11:02.630 And it's obviously a bonus for you, if you actually are new to programming. 212 00:11:02.630 --> 00:11:04.420 So, I'm gonna end this video here. 213 00:11:04.420 --> 00:11:07.790 In the next video we're going to continue our work on lists. 214 00:11:07.790 --> 00:11:09.870 And we're going to look at some different formats and 215 00:11:09.870 --> 00:11:13.140 ways to actually use replacement fields, lists, and so on and so forth. 216 00:11:13.140 --> 00:11:14.360 So, I'll see you in that next video.