1 00:00:00,660 --> 00:00:02,240 - [Instructor] In this self check exercise, 2 00:00:02,240 --> 00:00:04,530 we want you to go ahead and manipulate 3 00:00:04,530 --> 00:00:07,250 a two-dimensional list called t. 4 00:00:07,250 --> 00:00:09,320 And we've defined that list for you here. 5 00:00:09,320 --> 00:00:13,800 It has two rows, and in each row we have three columns each. 6 00:00:13,800 --> 00:00:15,280 Now, what we'd like you to do 7 00:00:15,280 --> 00:00:17,170 with this particular list, 8 00:00:17,170 --> 00:00:20,040 is first, go ahead and figure out the average 9 00:00:20,040 --> 00:00:23,900 of t's elements using nested for statements 10 00:00:23,900 --> 00:00:27,080 to walk your way through the list, calculate the total, 11 00:00:27,080 --> 00:00:30,230 and then eventually calculate the average. 12 00:00:30,230 --> 00:00:32,670 And then separately, we'd like you to do that 13 00:00:32,670 --> 00:00:33,650 a little bit differently, 14 00:00:33,650 --> 00:00:35,950 we'd like you to use a for statement 15 00:00:35,950 --> 00:00:38,500 that displays the average of t's elements 16 00:00:38,500 --> 00:00:42,200 using reductions like, sum and len 17 00:00:42,200 --> 00:00:44,357 to calculate the sum of each row's elements 18 00:00:44,357 --> 00:00:47,940 and the number of elements in each row respectively, 19 00:00:47,940 --> 00:00:50,100 and then move on to calculate the average 20 00:00:50,100 --> 00:00:53,030 as a result of all of that, as well. 21 00:00:53,030 --> 00:00:55,130 So, go ahead and give that a shot, 22 00:00:55,130 --> 00:00:58,143 and after you come back we'll show you the answer. 23 00:01:02,580 --> 00:01:04,470 Okay, so first of all, of course, 24 00:01:04,470 --> 00:01:06,360 we need to define the list 25 00:01:06,360 --> 00:01:08,600 that we were talking about up above. 26 00:01:08,600 --> 00:01:10,550 And the first the thing we want to do 27 00:01:10,550 --> 00:01:13,750 is used nested for loops to calculate 28 00:01:13,750 --> 00:01:18,130 the total and average for these elements. 29 00:01:18,130 --> 00:01:21,780 So, as you can see, we've defined a couple of counters here 30 00:01:21,780 --> 00:01:24,430 to help us keep track of the total of all the elements 31 00:01:24,430 --> 00:01:28,260 and to keep track of the number of items there are, as well. 32 00:01:28,260 --> 00:01:30,280 And we have a nested for loop, 33 00:01:30,280 --> 00:01:32,600 just like we showed you in the preceding video, 34 00:01:32,600 --> 00:01:34,200 but for each item this time, 35 00:01:34,200 --> 00:01:36,150 we're going to add the item to the total 36 00:01:36,150 --> 00:01:38,730 and we're going to increment items by one. 37 00:01:38,730 --> 00:01:40,050 And again, for those of you 38 00:01:40,050 --> 00:01:42,350 coming from other C-based languages, 39 00:01:42,350 --> 00:01:46,390 we do not have a plus plus operator in Python. 40 00:01:46,390 --> 00:01:48,520 So, let me go ahead and execute that. 41 00:01:48,520 --> 00:01:50,450 And, of course, once we have the total 42 00:01:50,450 --> 00:01:51,740 and the number of items, 43 00:01:51,740 --> 00:01:54,250 we can calculate the average very easily 44 00:01:54,250 --> 00:01:57,720 by just dividing the total by that number of items. 45 00:01:57,720 --> 00:02:00,740 Now, of course, we've seen a number of reductions 46 00:02:01,590 --> 00:02:04,270 in prior examples and lessons. 47 00:02:04,270 --> 00:02:07,820 And so there's easier ways to implement 48 00:02:07,820 --> 00:02:09,230 what we just showed you there. 49 00:02:09,230 --> 00:02:14,080 Let's go ahead and rest total to zero and items to zero. 50 00:02:14,080 --> 00:02:16,050 And take a look at this for loop. 51 00:02:16,050 --> 00:02:17,750 This is not a nested for loop, 52 00:02:17,750 --> 00:02:19,260 it's just a single for loop 53 00:02:19,260 --> 00:02:22,310 that says for every row in the array t, 54 00:02:22,310 --> 00:02:24,230 or in the list t, excuse me, 55 00:02:24,230 --> 00:02:27,570 let's go ahead and sum up the elements within that row 56 00:02:27,570 --> 00:02:30,700 and add that entire row's sum to the total. 57 00:02:30,700 --> 00:02:33,560 And let's count the number of items in that row 58 00:02:33,560 --> 00:02:35,070 by checking its length, 59 00:02:35,070 --> 00:02:37,550 and add that to the total number of items. 60 00:02:37,550 --> 00:02:41,280 And then, of course, after executing that we can calculate 61 00:02:41,280 --> 00:02:44,193 total divided by items to get our average.