1 00:00:00,790 --> 00:00:03,260 I know that you're really starting to love lists. 2 00:00:03,260 --> 00:00:06,080 And it's only a matter of time before you attempt to put one list 3 00:00:06,080 --> 00:00:07,630 inside of another list. 4 00:00:07,630 --> 00:00:11,980 And of course, you can do that, a list allows you to store any data type. 5 00:00:11,980 --> 00:00:14,000 And a list is just another data type. 6 00:00:15,490 --> 00:00:19,630 Now at first, this might not seem like it would be very beneficial. 7 00:00:19,630 --> 00:00:24,020 Until you realized that you can use this list inside of a list concept to represent 8 00:00:24,020 --> 00:00:25,790 rows and columns. 9 00:00:25,790 --> 00:00:28,600 The syntax may seem a little confusing at first. 10 00:00:28,600 --> 00:00:29,640 So I figured, 11 00:00:29,640 --> 00:00:33,030 you should get a tiny taste cuz you might see it out in a wild in code samples. 12 00:00:33,030 --> 00:00:34,700 Let's take a quick look. 13 00:00:34,700 --> 00:00:38,050 So let's imagine it's sometime in your near future and 14 00:00:38,050 --> 00:00:40,360 you're freelancing with your Python skills. 15 00:00:40,360 --> 00:00:42,025 You've been tracking your time and 16 00:00:42,025 --> 00:00:45,030 your expenses that you incur traveling to your client's site. 17 00:00:45,030 --> 00:00:47,750 You've been keeping your work log here in a spreadsheet. 18 00:00:47,750 --> 00:00:50,650 So here's an hours tab, these are how many hours you worked each of those days, and 19 00:00:50,650 --> 00:00:52,590 here's your travel, here's how much money it costs you. 20 00:00:52,590 --> 00:00:55,740 Sometimes you use public transit, sometimes you work from home, big savings. 21 00:00:56,770 --> 00:01:01,430 Do you see how it has rows of weeks and then each one of these columns here, 22 00:01:01,430 --> 00:01:03,890 these are the days of the week, right? 23 00:01:03,890 --> 00:01:07,295 So here is Tuesday and here is week two. 24 00:01:07,295 --> 00:01:11,400 Now let's take a look here at the values of week one. 25 00:01:11,400 --> 00:01:14,130 So here we go, here we go, week one, right? 26 00:01:15,290 --> 00:01:17,870 You can build a list like this pretty easily, right? 27 00:01:17,870 --> 00:01:19,990 I mean, basically just put some hard brackets and 28 00:01:19,990 --> 00:01:21,930 some commas between these values, right? 29 00:01:21,930 --> 00:01:26,640 So picture that list like a value as part of a bigger list, right? 30 00:01:26,640 --> 00:01:32,280 So there is item one, here is item two, here is item three, 31 00:01:32,280 --> 00:01:34,360 just three lists inside of the list. 32 00:01:36,310 --> 00:01:37,210 Let's do this. 33 00:01:37,210 --> 00:01:41,080 Let's go over to our work space and let's create a new file. 34 00:01:42,770 --> 00:01:47,618 We're gonna call that expenses.py. 35 00:01:47,618 --> 00:01:49,650 So go ahead and check out the teacher's notes. 36 00:01:49,650 --> 00:01:53,070 I went ahead and converted that worksheet so that we could use it in Python. 37 00:01:53,070 --> 00:01:57,920 Go ahead and copy and paste that, here we go. 38 00:01:57,920 --> 00:01:59,610 Travel expenses, excellent. 39 00:02:01,120 --> 00:02:05,370 So we've got for week 1 of Monday, we got 5, right? 40 00:02:05,370 --> 00:02:11,192 Let's just make sure, 5, 2.75, 22, 2.75, 22, awesome. 41 00:02:11,192 --> 00:02:16,350 So let's explore this list of lists using the shell. 42 00:02:16,350 --> 00:02:20,448 So I'll say python -i expenses. 43 00:02:21,829 --> 00:02:26,842 So let's see what happens if we get the length of that, length of travel expenses? 44 00:02:28,359 --> 00:02:31,160 3, okay, that makes sense. 45 00:02:31,160 --> 00:02:34,200 It's a list of lists and the length of the list, 46 00:02:34,200 --> 00:02:38,110 the outer list is how many top level elements there are. 47 00:02:38,110 --> 00:02:39,712 So there's 1, 2, 48 00:02:39,712 --> 00:02:43,870 3, there's three lists inside of this list, just like a normal list. 49 00:02:43,870 --> 00:02:46,190 I said list a lot didn't I? 50 00:02:46,190 --> 00:02:50,000 So then I suppose I can access the elements the way that we always do. 51 00:02:50,000 --> 00:02:53,945 So I should be able to get back that first week by just saying 52 00:02:53,945 --> 00:02:59,940 travel_expenses[0], and that should get the first week. 53 00:02:59,940 --> 00:03:02,000 Okay, it did, right, it pulled that list out. 54 00:03:03,050 --> 00:03:04,030 There we go. 55 00:03:04,030 --> 00:03:07,700 Now since that returns a list, I should be able to chain it. 56 00:03:07,700 --> 00:03:12,497 Like if I wanted to get this Tuesday, I wanted to get this 2.75, I could use 57 00:03:12,497 --> 00:03:17,754 travel_expenses(0) and then chain on the second element, which is 1, right? 58 00:03:17,754 --> 00:03:22,410 Starts at 0, so the first week of 1 should be here, right? 59 00:03:22,410 --> 00:03:23,200 So let's see what happens. 60 00:03:24,990 --> 00:03:28,430 Awesome, now this is the syntax that I was wanting you to see. 61 00:03:28,430 --> 00:03:32,320 Note how you first select the week, and then you select the day. 62 00:03:32,320 --> 00:03:35,390 Now this works, of course, with any row/column combination. 63 00:03:35,390 --> 00:03:41,650 These are dimensions, our data is multi dimensional, sounds outerspacey. 64 00:03:41,650 --> 00:03:43,680 Currently it's just two dimensions, right? 65 00:03:43,680 --> 00:03:47,140 So we have the first one, which is weeks and the second one, 66 00:03:47,140 --> 00:03:49,410 which is day of the week. 67 00:03:49,410 --> 00:03:56,370 So, I just got asked to report on how much travel expenses I am incurring weekly. 68 00:03:56,370 --> 00:04:00,620 So why don't we loop through the weeks and then calculate the total? 69 00:04:00,620 --> 00:04:01,550 So, let's do that up here. 70 00:04:01,550 --> 00:04:08,240 So we'll say print("Travel Expenses:"). 71 00:04:08,240 --> 00:04:10,970 So let's see, let's keep track of a counter for 72 00:04:10,970 --> 00:04:14,160 each week that we go through and we'll increment that in our loop. 73 00:04:14,160 --> 00:04:19,410 So we'll say the week_number, we'll start at week number one, okay? 74 00:04:19,410 --> 00:04:25,410 So, let's see, each row in that first dimension represented a week. 75 00:04:25,410 --> 00:04:28,330 So, let's name our variable that, that makes the most sense, right? 76 00:04:28,330 --> 00:04:33,270 So, for a week in travel_expenses and then each time through, 77 00:04:33,270 --> 00:04:35,060 it's going to pull that row. 78 00:04:35,060 --> 00:04:37,100 So, let's print out that out, 79 00:04:37,100 --> 00:04:42,720 we'll say print week, oops, there's a little bullet point there. 80 00:04:42,720 --> 00:04:47,850 And we'll do week #, and then we'll 81 00:04:47,850 --> 00:04:53,015 do that was that much money, and we will format that, 82 00:04:53,015 --> 00:04:58,380 and pass in the week_number. 83 00:04:58,380 --> 00:05:03,480 And there's a handy function called sum that takes an iterable and 84 00:05:03,480 --> 00:05:06,620 guess what our week is here, it's a list, so therefore, it's iterable. 85 00:05:06,620 --> 00:05:07,250 So let's just use it. 86 00:05:07,250 --> 00:05:09,490 We'll say sum, and you just pass it in iterable, and 87 00:05:09,490 --> 00:05:11,330 it will add it all up for us. 88 00:05:11,330 --> 00:05:13,620 So we will close the format statement there and 89 00:05:13,620 --> 00:05:16,950 we'll close the print statement, swesome. 90 00:05:16,950 --> 00:05:19,800 And now we gotta remember to increment this week number 91 00:05:19,800 --> 00:05:21,190 as we go through the loop each time. 92 00:05:21,190 --> 00:05:25,850 So we'll say week number +=1, save that. 93 00:05:25,850 --> 00:05:29,804 Let's go ahead, let's drop out here, let's give this thing a run. 94 00:05:33,682 --> 00:05:35,720 Awesome, pretty cool, right? 95 00:05:36,720 --> 00:05:38,660 Wanna know what's even cooler than that? 96 00:05:38,660 --> 00:05:43,620 You can actually export spreadsheets into a format that is known as CSV 97 00:05:43,620 --> 00:05:45,820 which stands for comma separated values. 98 00:05:45,820 --> 00:05:48,750 And there's a Python module that lets you read and 99 00:05:48,750 --> 00:05:52,700 process CSVs and it works much like this, rows and columns. 100 00:05:52,700 --> 00:05:55,550 Now, that module is out of the scope of this course, but 101 00:05:55,550 --> 00:05:56,670 check the teacher's notes for more. 102 00:05:57,740 --> 00:06:02,740 Now remember, these lists are still mutable, so make sure to be explicit 103 00:06:02,740 --> 00:06:06,830 when you change them and be careful about changing them while you're looping. 104 00:06:06,830 --> 00:06:09,292 I'm so glad that you had a chance to witness the power of 105 00:06:09,292 --> 00:06:11,400 multi-dimensional lists. 106 00:06:11,400 --> 00:06:16,120 Hopefully that syntax will not seem merely as intimidating when you see it next. 107 00:06:16,120 --> 00:06:20,560 All right, let's get to building our application right after this quick break.