Multidimensional Lists6:20 with Craig Dennis
You can put lists inside of lists. This allows for you to create dimensions like rows and columns. Come explore!
travel_expenses = [ [5.00, 2.75, 22.00, 0.00, 0.00], [24.75, 5.50, 15.00, 22.00, 8.00], [2.75, 5.50, 0.00, 29.00, 5.00], ]
I know that you're really starting to love lists. 0:00 And it's only a matter of time before you attempt to put one list 0:03 inside of another list. 0:06 And of course, you can do that, a list allows you to store any data type. 0:07 And a list is just another data type. 0:11 Now at first, this might not seem like it would be very beneficial. 0:15 Until you realized that you can use this list inside of a list concept to represent 0:19 rows and columns. 0:24 The syntax may seem a little confusing at first. 0:25 So I figured, 0:28 you should get a tiny taste cuz you might see it out in a wild in code samples. 0:29 Let's take a quick look. 0:33 So let's imagine it's sometime in your near future and 0:34 you're freelancing with your Python skills. 0:38 You've been tracking your time and 0:40 your expenses that you incur traveling to your client's site. 0:42 You've been keeping your work log here in a spreadsheet. 0:45 So here's an hours tab, these are how many hours you worked each of those days, and 0:47 here's your travel, here's how much money it costs you. 0:50 Sometimes you use public transit, sometimes you work from home, big savings. 0:52 Do you see how it has rows of weeks and then each one of these columns here, 0:56 these are the days of the week, right? 1:01 So here is Tuesday and here is week two. 1:03 Now let's take a look here at the values of week one. 1:07 So here we go, here we go, week one, right? 1:11 You can build a list like this pretty easily, right? 1:15 I mean, basically just put some hard brackets and 1:17 some commas between these values, right? 1:19 So picture that list like a value as part of a bigger list, right? 1:21 So there is item one, here is item two, here is item three, 1:26 just three lists inside of the list. 1:32 Let's do this. 1:36 Let's go over to our work space and let's create a new file. 1:37 We're gonna call that expenses.py. 1:42 So go ahead and check out the teacher's notes. 1:47 I went ahead and converted that worksheet so that we could use it in Python. 1:49 Go ahead and copy and paste that, here we go. 1:53 Travel expenses, excellent. 1:57 So we've got for week 1 of Monday, we got 5, right? 2:01 Let's just make sure, 5, 2.75, 22, 2.75, 22, awesome. 2:05 So let's explore this lists of lists using the shell. 2:11 So I'll say python -i expenses. 2:16 So let's see what happens if we get the length of that, length of travel expenses? 2:21 3, okay, that makes sense. 2:28 It's a list of lists and the length of the list, 2:31 the outer list is how many top level elements there are. 2:34 So there's 1, 2, 2:38 3, there's three lists inside of this list, just like a normal list. 2:39 I said list a lot didn't I? 2:43 So then I suppose I can access the elements the way that we always do. 2:46 So I should be able to get back that first week by just saying 2:50 travel_expenses(0), and that should get the first week. 2:53 Okay, it did, right, it pulled that list out. 2:59 There we go. 3:03 Now since that returns a list, I should be able to chain it. 3:04 Like if I wanted to get this Tuesday, I wanted to get this 2.75, I could use 3:07 travel_expenses(0) and then chain on the second element, which is 1, right? 3:12 Starts at 0, so the first week of 1 should be here, right? 3:17 So let's see what happens. 3:22 Awesome, now this is the syntax that I was wanting you to see. 3:24 Note how you first select the week, and then you select the day. 3:28 Now this works, of course, with any row/column combination. 3:32 These are dimensions, our data is multi dimensional, sounds outerspacey. 3:35 Currently it's just two dimensions, right? 3:41 So we have the first one, which is weeks and the second one, 3:43 which is day of the week. 3:47 So, I just got asked to report on how much travel expenses I am incurring weekly. 3:49 So why don't we loop through the weeks and then calculate the total? 3:56 So, let's do that up here. 4:00 So we'll say print("Travel Expenses:"). 4:01 So let's see, let's keep track of a counter for 4:08 each week that we go through and we'll increment that in our loop. 4:10 So we'll say the week_number, we'll start at week number one, okay? 4:14 So, let's see, each row in that first dimension represented a week. 4:19 So, let's name our variable that, that makes the most sense, right? 4:25 So, for a week in travel_expenses and then each time through, 4:28 it's going to pull that row. 4:33 So, let's print out that out, 4:35 we'll say print week, oops, there's a little bullet point there. 4:37 And we'll do week #, and then we'll 4:42 do that was that much money, and we will format that, 4:47 and pass in the week_number. 4:53 And there's a handy function called sum that takes an iterable and 4:58 guess what our week is here, it's a list, so therefore, it's iterable. 5:03 So let's just use it. 5:06 We'll say sum, and you just pass it in iterable, and 5:07 it will add it all up for us. 5:09 So we will close the format statement there and 5:11 we'll close the print statement, swesome. 5:13 And now we gotta remember to increment this week number 5:16 as we go through the loop each time. 5:19 So we'll say week number +=1, save that. 5:21 Let's go ahead, let's drop out here, let's give this thing a run. 5:25 Awesome, pretty cool, right? 5:33 Wanna know what's even cooler than that? 5:36 You can actually export spreadsheets into a format that is known as CSV 5:38 which stands for comma separated values. 5:43 And there's a Python module that lets you read and 5:45 process CSVs and it works much like this, rows and columns. 5:48 Now, that module is out of the scope of this course, but 5:52 check the teacher's notes for more. 5:55 Now remember, these lists are still mutable, so make sure to be explicit 5:57 when you change them and be careful about changing them while you're looping. 6:02 I'm so glad that you had a chance to witness the power of 6:06 multi-dimensional lists. 6:09 Hopefully that syntax will not seem merely as intimidating when you see it next. 6:11 All right, let's get to building our application right after this quick break. 6:16
You need to sign up for Treehouse in order to download course files.Sign up