Locked lesson.
About this lesson
Python lists are used to keep track of a list of items. They can be text, numbers, variables, other lists and more.
Exercise files
Download this lesson’s related exercise files.
8 - Python Lists.docx57 KB 8 - Python Lists SOLUTION.docx
55.5 KB
Quick reference
Python Lists
Python lists are used to keep track of a list of items. They can be text, numbers, variables, other lists and more.
When to use
Use them whenever you need to keep track of a list of things.
Instructions
To create a list, just name it and use square brackets:
names = ["John", "Bill", "Tina"]
Separate each item of the list by a comma. Text should be wrapped in quotation marks, numbers, variables, and other lists should not be wrapped in quotes.
List items are numbered, and start with zero.
To access an item in that list, simply reference its list number.
To reference John in the list above:
names[0]
Hints & tips
- names = ["John", "Bill", "Tina"]
- names[0]
- List items are numbered and start at zero.
- 00:04 Okay, in this video, I want to talk about Python Lists.
- 00:07 And Lists are a very important data structure in Python.
- 00:10 And they are just what they sound like, they're lists.
- 00:13 They are a way to keep track of different items.
- 00:16 So to create a list in Python, it's actually really easy.
- 00:19 Just like a variable, we name it.
- 00:21 So let's call this names and right now, this looks just like a variable.
- 00:26 And in fact it sort of is a variable.
- 00:27 It's just going to hold a list, a list of items.
- 00:31 So to create a list, we use these square brackets and now inside of these brackets,
- 00:35 we can have as many items as we want.
- 00:38 We just separate them all with commas.
- 00:40 So we're going to create a list of names here and let's create some.
- 00:43 So let's go John, let's go Tina, and let's go Bob.
- 00:50 And you'll notice I'm using double quotation marks.
- 00:53 You could also use single, it doesn't matter at all.
- 00:55 And let's go Tim.
- 00:57 So here we have a list of 1, 2, 3, 4 items.
- 01:02 Now the fun thing about lists are they are numbered.
- 01:05 So each of these things is a number and lists start at zero.
- 01:08 So John is 0, it's the zeroeth with item of our list.
- 01:12 Tina is the first item.
- 01:14 Bob is the second and Tim is the third.
- 01:16 Now that seems a little weird.
- 01:17 Tina is not the first, it's obviously the second item, but
- 01:21 like I said, lists start at 0, so 0,1,2 and 3.
- 01:25 So to access our list, we can do it several ways.
- 01:29 So let's just print our list to the screen.
- 01:31 First off, let's just print out the whole thing.
- 01:32 So print (names).
- 01:34 Let's head back over to our terminal and let's run this.
- 01:38 And we see we get this whole list of things and
- 01:40 the square brackets are there and the quotation marks are all there.
- 01:43 And that's not particularly useful.
- 01:45 So you usually don't just call the entire list when you have a list.
- 01:50 Instead, you call out specific items of that list.
- 01:54 So to do that, we just reference their index number.
- 01:57 And we do that by slapping some square brackets to the end of our variable
- 02:02 name or list name, and just calling out the list item.
- 02:06 So if we want to reference John, that's the zeroth item,
- 02:09 we would just type names and then put a zero in there.
- 02:12 So let's save this and run it, see what that looks like.
- 02:16 And you'll notice it just prints out John.
- 02:18 If we want Tina, Tina is the first item.
- 02:21 So we could save this and run it.
- 02:24 And when we do it prints out Tina.
- 02:27 All right, very cool.
- 02:30 So, lists are super useful.
- 02:32 We're going to use these over and over again.
- 02:34 And think about Excel, there's often rows and columns of information and
- 02:38 data you want to get out.
- 02:40 Sometimes we'll use lists to pull out those items to represent those items.
- 02:44 So we'll grab everything from an Excel spreadsheet and
- 02:47 we'll add it to a list if it makes sense data-wise for that.
- 02:51 So if we have for instance one column of things,
- 02:54 we would pull out every item in that column and put it in a list.
- 02:57 Now, a lot of times Excel data is more complicated than just single items
- 03:01 like this.
- 03:02 So the list isn't as useful but sometimes it is and it's good to know and so
- 03:07 that's the list.
- 03:08 So very quickly before we end this video, we can delete things in our list.
- 03:15 So we can call the delete function, and then we can reference names and
- 03:18 let's say we want to delete John.
- 03:21 That's the zeroth item.
- 03:22 So now if we run this, we should just see Tina, Bob and
- 03:26 Tim because John is being deleted.
- 03:28 So let's save this and run it and see.
- 03:30 And sure enough, Tina, Bob and Tim john is no longer there.
- 03:35 So that's interesting.
- 03:38 Let's get rid of that.
- 03:39 We can change things.
- 03:41 So if we want to change John to something else, we can just call names and then
- 03:46 reference the thing we want to change, and then set it equal to something else.
- 03:51 So we could code James.
- 03:53 So let's go ahead and save this and run it.
- 03:55 And now we see James, Tina, Bob and Tim.
- 03:59 So that's kind of cool.
- 04:02 Now we're putting strings in here.
- 04:04 We could put numbers in here too.
- 04:06 Notice there's no quotation marks.
- 04:07 As I mentioned in the last video, we never put quotation marks around numbers.
- 04:11 So here 0 1 2 3.
- 04:14 So if we referenced the third item,
- 04:18 it will print out 41 hopefully.
- 04:22 Sure enough 41.
- 04:24 So that's cool.
- 04:26 We can put other variables.
- 04:27 We can go first_name = John and then instead of putting it
- 04:33 explicitly like this, we can reference the variable in here.
- 04:39 So now if we print out names first_name, what do you think it'll print out?
- 04:43 Will it print out first_name or will it print out John?
- 04:47 It should print out John.
- 04:50 And there we go.
- 04:51 So we can put words, we can put strings, texts, numbers,
- 04:55 we can put other lists in there.
- 04:56 So let's call this nums and let's go 1,2,3,4.
- 05:01 So here instead of 41, we can put that whole list.
- 05:06 So now this is 0, 1, 2, 3.
- 05:08 If we reference this third item, it should be this whole list.
- 05:14 This is fun.
- 05:14 And you see there's the whole list.
- 05:18 We can then further reference then things inside of this
- 05:21 list if we want by just slapping another set of brackets on here.
- 05:25 And let's say we want the zeroth item which is 1, we could just do it like that.
- 05:31 If we save this and run it, it prints out 1.
- 05:35 So lists are very, very useful.
- 05:37 And like I said, we're probably going to use these quite a bit throughout this
- 05:40 course as we're pulling data out of our Excel spreadsheets.
- 05:43 They're not that complicated, but like I said, very useful and a lot of fun.
- 05:46 So that's all for this video and the next video we'll look at tuples.
Lesson notes are only available for subscribers.