WEBVTT 1 00:00:02.240 --> 00:00:05.710 So, in the previous video we started with an introduction to lists, and 2 00:00:05.710 --> 00:00:09.180 we went through various scenarios to sort of see how we could manipulate some 3 00:00:09.180 --> 00:00:12.390 basic lists and indeed actually talked about what lists were. 4 00:00:12.390 --> 00:00:15.960 So, as it turned out we went through three ways to create a list, 5 00:00:15.960 --> 00:00:20.500 the first was enclosing the item in square brackets by concatenating existing lists, 6 00:00:20.500 --> 00:00:23.720 adding two together, and also by using a function that returns a list, 7 00:00:23.720 --> 00:00:26.440 which I used in this example, sorted. 8 00:00:26.440 --> 00:00:30.795 We can also use the list type constructor, which is like a list in brackets, 9 00:00:30.795 --> 00:00:31.665 with brackets. 10 00:00:31.665 --> 00:00:34.555 And constructors will make more sense when I've introduced classes, 11 00:00:34.555 --> 00:00:36.675 which will be in a later section of this course. 12 00:00:36.675 --> 00:00:40.285 But for now, just think of the list constructor as just another function that 13 00:00:40.285 --> 00:00:41.275 you can actually call. 14 00:00:41.275 --> 00:00:44.805 So, if you use list with no parameters it just creates an empty list. 15 00:00:44.805 --> 00:00:45.925 So, I'm going to delete this code and 16 00:00:45.925 --> 00:00:47.955 we're going to start with a clear slate here. 17 00:00:50.380 --> 00:00:51.190 Like so. 18 00:00:51.190 --> 00:00:55.010 So, using a list as I said with no parameters, it just creates an empty list. 19 00:00:55.010 --> 00:00:59.290 So we can type, and that ends up being exactly the same as the first option here. 20 00:00:59.290 --> 00:01:04.688 So, I've got list_1 = [], which is an empty list, 21 00:01:04.688 --> 00:01:09.450 but I can also do this, list_2 = list[]. 22 00:01:09.450 --> 00:01:12.730 And in both cases, what they do is they create an empty list. 23 00:01:12.730 --> 00:01:14.490 So basically, they're both identical as you can see. 24 00:01:14.490 --> 00:01:17.270 And just to confirm that, we can actually print this out. 25 00:01:17.270 --> 00:01:24.120 I can type print, list1.format. 26 00:01:24.120 --> 00:01:27.299 This data's called 1, and 27 00:01:27.299 --> 00:01:32.470 print list 2.format, this data's got 2. 28 00:01:32.470 --> 00:01:40.204 And I can put if list_1 =, this data's called 2. 29 00:01:42.290 --> 00:01:47.620 Print the list are equal, we can actually run this. 30 00:01:50.135 --> 00:01:53.070 We can see that both are empty lists, list 1 and list 2 are both empty. 31 00:01:53.070 --> 00:01:56.670 So, you can see there and we get confirmation by doing the comparison 32 00:01:56.670 --> 00:01:59.330 the double equals sign to compare them, and 33 00:01:59.330 --> 00:02:01.510 they are both considered equal by Python. 34 00:02:01.510 --> 00:02:04.374 Now ,I don't think we've actually mentioned this before, but 35 00:02:04.374 --> 00:02:07.410 actual, when you call a function such as print or sorted. 36 00:02:07.410 --> 00:02:09.750 So, function in this example being print. 37 00:02:09.750 --> 00:02:11.860 You can see this stuff in the middle here. 38 00:02:11.860 --> 00:02:14.640 They're actually called parameters or arguments. 39 00:02:14.640 --> 00:02:17.190 So, the parameter in our last print function code was the string 40 00:02:17.190 --> 00:02:17.990 the lists are equal. 41 00:02:17.990 --> 00:02:20.000 So, this is actually a parameter. 42 00:02:20.000 --> 00:02:22.850 That's a parameter that we're passing to the function called print. 43 00:02:22.850 --> 00:02:26.470 But later in the course, we'll start actually creating our own functions, and 44 00:02:26.470 --> 00:02:29.610 we'll actually cover parameters and arguments then in more detail. 45 00:02:29.610 --> 00:02:32.680 But strictly speaking, the lists are not equal is an argument. 46 00:02:32.680 --> 00:02:37.000 But the two terms, argument and parameters are often used interchangeably. 47 00:02:37.000 --> 00:02:40.570 So, the list constructor, and again the constructor is using the list 48 00:02:40.570 --> 00:02:44.170 with the brackets, can be called with no parameters as we've done. 49 00:02:44.170 --> 00:02:47.430 We can also call it with a single iterable parameter. 50 00:02:47.430 --> 00:02:51.020 So, in Python an iterable is just an object that is capable of 51 00:02:51.020 --> 00:02:53.650 returning its members one at a time. 52 00:02:53.650 --> 00:02:57.120 So, all the sequence types built into Python are actually iterable, and 53 00:02:57.120 --> 00:03:01.180 we can actually pass a string to the list, so we could do something like this. 54 00:03:01.180 --> 00:03:09.340 print(list("The lists are equal.")) like so, we can run that. 55 00:03:09.340 --> 00:03:13.130 And you can see what's happened there is automatically, 56 00:03:13.130 --> 00:03:17.180 Python has actually created a list from what we sent to one character at a time. 57 00:03:17.180 --> 00:03:21.320 So each item, each letter, of this string, 58 00:03:21.320 --> 00:03:24.960 has been added individually, as a separate option in that actual list. 59 00:03:24.960 --> 00:03:26.600 And that's what the constructor's actually doing. 60 00:03:26.600 --> 00:03:29.490 It's actually doing that for us, and I'm recalling that list function. 61 00:03:29.490 --> 00:03:33.270 So, creating a list whose members are each of the characters in the string 62 00:03:33.270 --> 00:03:34.230 that we passed to the list. 63 00:03:34.230 --> 00:03:37.290 And this was the lists are equal, is what we actually passed to it. 64 00:03:37.290 --> 00:03:40.070 So it can actually be very useful to do that, if you wanna assign a list to 65 00:03:40.070 --> 00:03:43.480 another variable, and have them then reference different lists. 66 00:03:43.480 --> 00:03:47.761 So, a normal assortment works as in other languages, and it can actually confuse 67 00:03:47.761 --> 00:03:51.856 newcomers to programmers, as I'm gonna show in the following example, so, 68 00:03:51.856 --> 00:03:54.665 I'm gonna type in some code here, comment this out. 69 00:03:54.665 --> 00:04:00.555 We can put even = [2, 4, 6, 8]. 70 00:04:00.555 --> 00:04:04.170 Then, we type another, even = even. 71 00:04:04.170 --> 00:04:09.499 Then we can type another, even.sort(reverse=True), 72 00:04:09.499 --> 00:04:12.600 note the capital T for True. 73 00:04:12.600 --> 00:04:15.640 We can do print(even), so if we actually run this, 74 00:04:15.640 --> 00:04:21.050 you can see what's actually happened there. 75 00:04:21.050 --> 00:04:24.070 We've actually got it printing out even, even though, 76 00:04:24.070 --> 00:04:26.640 we're actually updating another variable. 77 00:04:26.640 --> 00:04:31.230 So, we've created another_even variable and assigned it to even. 78 00:04:31.230 --> 00:04:34.820 Then, we actually updated the values of even using sort. 79 00:04:34.820 --> 00:04:39.270 And we actually used the reverse equal true parameter to actually 80 00:04:39.270 --> 00:04:43.030 return the results sort of last to first, instead of first to last. 81 00:04:43.030 --> 00:04:47.150 But the important thing here was that, even though we haven't actually updated 82 00:04:47.150 --> 00:04:50.500 even directly we've used another variable called even. 83 00:04:50.500 --> 00:04:54.330 Because we've updated that variable it actually updated the variable 84 00:04:54.330 --> 00:04:55.930 even as well. 85 00:04:55.930 --> 00:04:58.820 So, in other words both variables even and another even 86 00:04:58.820 --> 00:05:03.832 refer actually to the exact same list so that and that's why when we used the sort, 87 00:05:03.832 --> 00:05:07.010 both [INAUDIBLE] were actually sorted as you can see, because we're actually 88 00:05:07.010 --> 00:05:11.520 printing out the first list, which we didn't actually directly use the .sort on. 89 00:05:11.520 --> 00:05:14.900 And just to confirm that what we can actually do, after the assignment we can 90 00:05:14.900 --> 00:05:20.330 confirm that they're the same by typing print, another_even is even. 91 00:05:21.850 --> 00:05:23.900 And that should actually return true. 92 00:05:23.900 --> 00:05:26.780 So if we actually run that, it actually returns true, 93 00:05:26.780 --> 00:05:30.760 which actually says from Python's perspective these two are the same thing. 94 00:05:30.760 --> 00:05:32.800 They're actually pointing to the same list. 95 00:05:32.800 --> 00:05:38.110 But if we actually create a new list for another even variable, 96 00:05:38.110 --> 00:05:41.590 and pass in the parameter of even, we can get that not to be the case. 97 00:05:41.590 --> 00:05:44.130 So we can actually type list here, the function list, 98 00:05:44.130 --> 00:05:48.900 well the constructor list, we're passing even, that current list to that, and 99 00:05:48.900 --> 00:05:50.890 list is going to return, in fact, a new list. 100 00:05:50.890 --> 00:05:54.450 So, when we do this print down here, we should find that that comes up and 101 00:05:54.450 --> 00:05:55.720 says false. 102 00:05:55.720 --> 00:05:57.900 But we still will end up getting the same results, 103 00:05:57.900 --> 00:06:00.290 because we're still ultimately sending the same information. 104 00:06:00.290 --> 00:06:04.000 So, if we actually run this, you can see that's now false. 105 00:06:04.000 --> 00:06:05.830 And that's because they're no longer the same list. 106 00:06:05.830 --> 00:06:07.830 We had a list that was created for even. 107 00:06:07.830 --> 00:06:12.860 And then we used the list constructor, passing the even list. 108 00:06:12.860 --> 00:06:15.990 And the list constructor actually returns us a new list. 109 00:06:15.990 --> 00:06:18.310 So, consequently, there's two separate lists now. 110 00:06:18.310 --> 00:06:20.190 The even variable has got its own list. 111 00:06:20.190 --> 00:06:24.220 And the other _even variable has also got its own list, as well. 112 00:06:24.220 --> 00:06:27.610 Just to confirm, we can actually do another test there and print another. 113 00:06:27.610 --> 00:06:30.360 Instead of putting is we can put equals equals then actually run that. 114 00:06:31.610 --> 00:06:34.270 That's gonna tell us to say at this point now it's true, 115 00:06:34.270 --> 00:06:38.100 because that's saying whether the contents are equal, not whether they're 116 00:06:38.100 --> 00:06:41.330 actually pointing to different variables in memory if that makes sense, 117 00:06:41.330 --> 00:06:42.500 in different lists essentially. 118 00:06:42.500 --> 00:06:46.190 So, it's important to know, The difference there between the double equal. 119 00:06:46.190 --> 00:06:50.070 So, we're actually comparing it to see whether what's in the lists are the same 120 00:06:50.070 --> 00:06:51.120 as supposed is. 121 00:06:51.120 --> 00:06:54.780 We're saying is this the same list that's actually in memory somewhere. 122 00:06:54.780 --> 00:06:56.455 So, that's gonna return false, cause it's separate now. 123 00:06:56.455 --> 00:07:00.275 And again, equals is the contents of the lists the same. 124 00:07:00.275 --> 00:07:01.115 That's gonna return true. 125 00:07:01.115 --> 00:07:04.365 Now, if you really wanted a new version of even, the first variable, 126 00:07:04.365 --> 00:07:07.735 we could have actually done something like this, where instead of putting list, 127 00:07:07.735 --> 00:07:10.975 we could have actually done something like sorted, even, 128 00:07:12.175 --> 00:07:16.910 reverse equals true, true, we could run that. 129 00:07:16.910 --> 00:07:20.830 And that's now false, because we sorted it into a different order. 130 00:07:20.830 --> 00:07:24.830 So, sorter does return a new list, but now it's sorting it in a different order. 131 00:07:24.830 --> 00:07:28.850 And just to confirm, I set that to be is and run it. 132 00:07:28.850 --> 00:07:30.970 It's still set to false, 133 00:07:30.970 --> 00:07:33.770 because they're actually pointing to different lists still. 134 00:07:35.170 --> 00:07:38.870 And setting that to equals we correctly get false as well, 135 00:07:38.870 --> 00:07:42.260 and that's because the sorted function returns a separate list. 136 00:07:42.260 --> 00:07:42.850 So moving on. 137 00:07:42.850 --> 00:07:46.280 So earlier, we concatenated or appended our even and odd list. 138 00:07:46.280 --> 00:07:47.440 That's actually in a previous video. 139 00:07:47.440 --> 00:07:49.880 And we actually produced a new list called numbers, 140 00:07:49.880 --> 00:07:51.710 that included all the items from both lists. 141 00:07:51.710 --> 00:07:53.090 I'm gonna put some more code on the screen. 142 00:07:53.090 --> 00:07:54.480 And see, if you can actually guess what this will do. 143 00:07:54.480 --> 00:07:56.710 So, I'm gonna comment that out and put the even in there. 144 00:07:58.460 --> 00:08:02.020 And we're gonna put an odd equals 1, 3, 5, 7, 9 and 145 00:08:02.020 --> 00:08:07.140 this is from the previous video when we're dealing with adding these things together. 146 00:08:07.140 --> 00:08:14.787 What do you think is gonna happen if I put numbers equals even, odd and print it out? 147 00:08:16.452 --> 00:08:19.220 So, can you guess what's that gonna actually produce? 148 00:08:19.220 --> 00:08:19.940 Let's run that and see. 149 00:08:20.960 --> 00:08:23.710 Now, it actually returns a list containing two lists. 150 00:08:23.710 --> 00:08:25.970 So, you can see, sort of lists within a list. 151 00:08:25.970 --> 00:08:30.110 That's the list number one, which is based on the contents of even, and 152 00:08:30.110 --> 00:08:34.880 the second one is based on the contents of odd, and now are both appended into in 153 00:08:34.880 --> 00:08:37.850 one master list, and just to confirm that, instead of actually printing that out, 154 00:08:37.850 --> 00:08:41.070 let's actually change that and actually add some code here and put a for loop. 155 00:08:41.070 --> 00:08:45.235 So, for numbers_set in numbers. 156 00:08:47.265 --> 00:08:48.223 Print. 157 00:08:51.058 --> 00:08:51.860 Number set. 158 00:08:51.860 --> 00:08:54.320 And it actually should have been number set just to be technically correct. 159 00:08:55.350 --> 00:08:58.850 Like so. And then, we'll put for, 160 00:08:58.850 --> 00:09:06.080 noting the indent level for value in number_set.print(value). 161 00:09:06.080 --> 00:09:12.060 So, in the outer loop number_set iterates through the members of numbers, 162 00:09:12.060 --> 00:09:14.530 so it's assigned each of the two lists in turn. 163 00:09:14.530 --> 00:09:18.210 And within the inner loop itself the value iterates through the members of those 164 00:09:18.210 --> 00:09:21.780 sets, so it's assigned each of the even numbers first time around 165 00:09:21.780 --> 00:09:24.300 then each of the odd ones in the second time around. 166 00:09:24.300 --> 00:09:25.110 So if we actually run that, 167 00:09:25.110 --> 00:09:27.920 we can actually see we can go right through the list. 168 00:09:27.920 --> 00:09:31.070 We can go through and print out the number set first. 169 00:09:31.070 --> 00:09:32.090 This is the initial print. 170 00:09:32.090 --> 00:09:35.210 And then, the second follow-up is going through each of those numbers 171 00:09:35.210 --> 00:09:37.000 within that list and printing them out. 172 00:09:37.000 --> 00:09:39.760 It does that for both lists because we've got an initial for 173 00:09:39.760 --> 00:09:43.140 loop, the outside one, which actually goes through all the contents. 174 00:09:43.140 --> 00:09:44.460 So, we're actually gonna end this video here. 175 00:09:44.460 --> 00:09:45.600 In the next video I'm gonna go through and 176 00:09:45.600 --> 00:09:51.070 just show you a few more examples of using lists the way we've been using them. 177 00:09:51.070 --> 00:09:53.700 And we'll actually then introduce you to a mini challenge to finish that off. 178 00:09:53.700 --> 00:09:54.927 So, I'll see you in the next video.