Indexing10:33 with Craig Dennis
Now that we have stuff in our list, how do we access specific elements. Through indexing! Let's explore.
books = [ "Automate the Boring Stuff with Python: Practical Programming for Total Beginners - Al Sweigart", "Python for Data Analysis", "Fluent Python: Clear, Concise, and Effective Programming - Luciano Ramalho", "Python for Kids: A Playful Introduction To Programming - Jason R. Briggs", "Hello Web App: Learn How to Build a Web App - Tracy Osborn", ]
- PEP8 - When to Use Trailing Commas (Immerse yourself...reminder, it's okay if you don't understand everything!)
- Great StackOverflow Answer on why to use trailing commas
- Rapping Raisins - Books, Check 'em out (Yes, that is Sir Mix-A-Lot)
There is gonna come a time when you're working with lists, 0:00 where you're gonna want to access a specific element in that list. 0:03 Now lucky for us, 0:06 you can refer to a specific element through what is known as indexing. 0:07 Every element in your list is assigned a number. 0:12 And you can access them by an opening hard bracket followed by a number 0:14 representing which element you want and a closing hard bracket. 0:18 We'll explore accessing elements here in a bit but 0:22 first, let's tackle a common problem that happens when indexing. 0:24 Indexing in Python is zero based. 0:28 What this means is that the first element, 0:31 which most likely you would assume to be one, is actually zero. 0:33 And the second element that you would probably assume is two, it's actually one. 0:37 I can't tell you the amount of times that I've made 0:42 the mistake of using an incorrect index. 0:44 We all do. 0:47 It feels unnatural. 0:48 And we all struggle with it and I think I found the way to help you remember it. 0:49 We do it in English too. 0:54 So take a moment and think about how we talk about age. 0:56 Now, my youngest daughter, she's about to turn four. 0:59 Her first year of life though, 1:01 like before her first birthday, we talked about how old she was in terms of months. 1:03 It was never like, hey how old is this cutie? 1:08 Zero, but really it was her zeroth year. 1:11 After she turned one, we just say one and then two and now she's three. 1:14 We don't really ever say it but 1:18 to access that first year of her life by a year index, I'd use zero too. 1:20 Now, as weird as it sounds in baby years, 1:25 it's okay to say the zeroth in indexing, no matter how awkward it sounds. 1:27 You'll get used to it. 1:31 Let's go look at how to get the zeroth or first element. 1:32 One of my favorite types of list is a wish list. 1:38 I have a wish list on most every site that allows for them. 1:41 I keep track of the stuff that I wanna buy. 1:44 And when it comes time for presents, I just point people to it. 1:46 I love to read books, especially programming books. 1:49 Let's use my Python reading wish list as an example. 1:52 So, let's go ahead and create a new file called wishlist.py. 1:56 And in the teacher notes of this video I've included the list. 2:04 So go ahead and copy it. 2:08 And then come in here and paste it. 2:10 Go ahead and save it. 2:16 Whenever I get a new recommendation, I just append it to my list. 2:18 I really enjoy reading beginner books, and 2:21 the creative ways that authors come up with presenting the challenging concepts. 2:24 This teaching programming is pretty hard. 2:27 And it's kind of an addiction of mine. 2:30 I always read these books. 2:31 And I keep hearing such great things about this first one, 2:33 this Automate the Boring Stuff with Python. 2:35 I've heard so many people love this book, I can't wait to read it. 2:37 One thing that I'd like to point out here, is look at how nicely things are spaced. 2:40 Each item is on its own line. 2:44 This is pretty common syntax, and you can see that it improves legibility. 2:47 Now imagine if this were all on one line. 2:50 It would be a little gross, right? 2:52 It's definitely hard to parse as a human. 2:54 But this way is really nice. 2:56 Another thing that you'll notice is I have a trailing comma here after this 2:57 last element. 3:01 This is also totally fine and even encouraged. 3:02 It makes adding a new item to the list that much easier. 3:05 Check the teacher's notes for more. 3:08 So, I wanna explore this list a bit and I wish I had this list in the repo. 3:10 Ready for this? 3:14 You can actually run this code interactively. 3:15 Meaning the code will execute and then you're dropped into a shell. 3:18 And all you need to do to make that happen is to pass a specific option or 3:22 flag to the interpreter. 3:26 And that flag is dash i for interactive. 3:27 So what you do is python -i, for interactive and 3:30 you say the name of the file, wishlist. 3:34 And you'll see that I've been dropped into the shell, and 3:38 I have access now to our books list. 3:41 Check this out. 3:42 Boom, super powerful, right? 3:44 Also see have ugly that single line representative of this list looks? 3:46 It's not very pretty. 3:50 So let's go ahead and retrieve that first element. 3:53 So we have books, and in order to get inside, 3:57 we use an opening hard bracket and we want to get the first element. 4:02 So we remember that lists are zero-based, so 4:07 we use 0 and then a closing hard bracket. 4:10 You'll see, it returns the element in the list at position zero. 4:15 Now the zero is also referred to as the index. 4:20 Now, one thing to remember is that lists are mutable. 4:25 So I can change this list in a couple of ways. 4:28 Now one way, is that I can assign values to it. 4:30 So actually, look at this, our second entry here this Python for data analysis. 4:33 I forgot the author. 4:39 Whoops, so I can edit that by access the element. 4:40 So that's books, and now we want the second entry, so that's two, right? 4:44 No, it's zero based, it's in baby years, that's right. 4:49 So the second is actually one. 4:53 And now I can just assign to it. 4:55 So that is Python for data analysis. 4:57 And the author is super important on this one. 5:03 It's Wes McKinney. 5:05 He is actually the creator of the data framework Pandas. 5:08 And Pandas takes up a good portion of this book, this Python for data analysis book. 5:12 And it's straight from the creator. 5:16 We can't forget Wes. 5:17 More in the teacher's notes. 5:19 So note now that books one has been updated and 5:20 if we look at our books list our books list is updated. 5:24 So there's Wes on our book list. 5:26 I think I'm gonna go ahead and copy this and 5:30 fix this in our list, because I don't want to forget that later. 5:33 So what we've done is we've changed something in the shell, but 5:37 we haven't changed it in our code. 5:40 So I'm gonna change it here, and I've put it up here. 5:41 There we go. 5:44 A wonderful feature of lists is that you can 5:45 index them with a negative index to count from the end. 5:48 So the last item in our list is always going to be negative one. 5:52 So if we look at books negative one 5:56 we'll see that we get back hello web app from Tracy Osborn. 5:59 And we can get the second to last book by doing books negative two and 6:03 we get the python for kids. 6:09 I can't wait to do that with my daughter, she's not quite there yet, reading wise 6:10 which is why it's close to the end of my list, but I can not wait to do that one. 6:14 Now if you didn't know that you could use negative indexing on lists, 6:18 how do you think that you might try to get to that last element? 6:21 What would you do to accomplish it? 6:24 Now, it might not be actually clear, but you can run code inside of the index. 6:26 You can run code in here. 6:32 So if you said books, and if we wanted to get the last element, 6:33 I guess what we would do is we get the length of books. 6:36 And then we would subtract one because the length of books would be 6:41 five here, but we only wanna go zero, one. 6:46 This is zero, one, two, three, four. 6:49 So we wanna get the last ones. 6:50 So we'd say books. 6:53 So you can actually run code in there, and 6:55 you'll see that it still pulls the last one out. 6:57 We don't need to do that because it's actually negative one. 6:59 But I did want you to see that you could run code to produce an index. 7:02 We really should just rely on those negative indexes, which by the way, 7:06 is the multiple of index, indexes. 7:10 So let's add a little feature to our wish list script. 7:13 Let's print out the recommended gift for anyone looking to buy us something, right? 7:15 So we'll say, print suggested gift and 7:21 we'll use the string formatting. 7:26 And we'll put the first element there which again is zero. 7:31 There we go. And I'm gonna go ahead and 7:38 I'm gonna drop out of here. 7:40 Let's go ahead and run this. 7:43 I forgot to save it. 7:47 So let's go ahead here and we'll save it, and then we'll run it, 7:48 Awesome, automate the boring stuff, nice recommendation. 7:57 Now so far we've seen that you can use append to add to the end of the list. 8:02 Well now that we know about how indices work I'd like to show you another way to 8:06 add an item to your list. 8:11 So let's add a new book but to the front of the list. 8:12 We want to insert a book at a specific point in our ordered list. 8:15 So, the key word is insert. 8:19 So, we're going to say books.insert. 8:21 And, we'll insert zero here, and we'll do a Learning Python. 8:24 So, it's a gigantic book with an unfortunate acronym, 8:29 Powerful Object-Oriented Programing. 8:35 And so now if we look at books, we'll see, there we go it's there at the front, 8:41 inserted. 8:46 This book is huge. 8:48 It's on its fifth edition. 8:49 And shoot [LAUGH], I forgot to add the author. 8:50 That's okay. 8:53 I can assign elements and I can even use in place additions. 8:53 So let's say, books zero now. 8:57 We're gonna do plus equals and because we want the title, 8:59 I'm gonna do Mark Lutz, cool right? 9:03 It works just like a variable. 9:08 You know what? 9:10 Now that you know about indexing, check this out on strings. 9:12 So, in the 90's there was this bizarre time 9:15 where raisins were rapping about using the library. 9:19 Now, I can hardly say the word books with out rapping. 9:23 Books check them out, one of the teacher's notes. 9:26 So those lyrics to that song are books 9:31 check them out, like that. 9:36 Now, I can access characters of a string by index. 9:41 So I could say lyrics, zero, and we'll get B, the first letter from that. 9:45 Cool, right? 9:51 Now the thing to remember is that, while our lists are mutable, we can change them. 9:52 Strings are immutable, I can't change them. 9:56 So like for instance if I wanted to change the first letter here to a C, 10:00 if we wanna say the lyrics zero equals C, makes that cooks check him out. 10:04 You'll notice it won't let me, cuz it's immutable. 10:10 All right, so we've got a wish list, but 10:14 no way to do the most important part, grant a wish. 10:18 We have to be able to get those items out of the wish list, right? 10:22 Also, as we know, we might make a mistake if we want to delete an item. 10:25 Let's take a look at removing items right after this quick break. 10:30
You need to sign up for Treehouse in order to download course files.Sign up