Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Lists.docx58.9 KB Lists - Solution.docx
58.9 KB
Quick reference
Lists
Lists are exactly what they sound like...lists of items.
When to use
Use a list whenever you have items that you can store numerically.
Instructions
A list is...well...a list! To create one:
my_list = ["John", "Tim", "Mary"]
Lists can contain strings, numbers, variables, other lists, and just about anything else you can think of.
Items in a list are accessed by their numeric index number.
List item index numbers start with zero, and increase by one for each item in the list.
To print out an item in a list:
print(my_list[0])
To delete the first item in a list (or any item):
del my_list[0]
To add an item to the end of a list:
my_list.append("item to add")
Hints & tips
- my_list = ["John", "Tim", "Mary"]
- print(my_list[0])
- List item index numbers start at zero
- 00:04 In this video, I want to talk about lists.
- 00:07 And lists are one of our five main data types that we talked about earlier and
- 00:11 they are exactly what they seem.
- 00:12 They are a list of things and sort of like a variable.
- 00:16 You can put one thing in a variable, in a list,
- 00:18 you can put as many things as you want.
- 00:20 And they can all be different types of things.
- 00:23 You could put strings, you could put numbers, you could put other lists, and
- 00:25 on and on and on.
- 00:26 So if you're familiar with any sort of computer programming besides Python,
- 00:31 you're going to recognize a list as what everybody else calls an array.
- 00:35 So basically, a list is an array, Python decided to call it lists.
- 00:39 I guess that's fine, makes more sense, I supposed.
- 00:42 But anyway, lists are very easy to create.
- 00:44 And they're very similar to tuples which we'll talk about
- 00:47 in a couple of videos from now, the main difference between a list and
- 00:50 a tuple is that lists can be changed.
- 00:53 You can add things, you can remove things, you can do all kinds of things and
- 00:56 tuples can't.
- 00:56 So that's the main difference.
- 00:58 So let's go ahead and create one real quick and
- 01:00 let's just call this first_names and so far it looks just like a variable, right?
- 01:06 We just write a name, set an equal to sign.
- 01:08 Now to create a list we use these square brackets and everything inside of those
- 01:12 brackets will be the items in our list, and we separate those by commas.
- 01:17 So let's add John, and you see I'm using double quotation marks,
- 01:21 you could use single as well.
- 01:22 Let's go, Tim and let's go, Mary.
- 01:26 So we can print out the entire list just by doing print and then name it.
- 01:32 If we save this and run it, we see John, Tim and
- 01:35 Mary stranded by our square brackets.
- 01:37 This list is ordered, right?
- 01:39 And it's numerically ordered and the numbers start at 0.
- 01:42 So John is 0, Tim is 1 and Mary is 2.
- 01:46 That confuses a lot of people because you think John is one,
- 01:49 you just got to remember that lists start at 0.
- 01:52 So if we want to print out the first item, just put 0, save this, John.
- 01:58 We can also use our Len statement like we used
- 02:03 couple of videos ago to find out exactly how many items are in our list.
- 02:07 So we can see three.
- 02:08 So if we wanted, say the last item, this is the 0, 1, 2.
- 02:14 The second item, Mary is the second.
- 02:16 We know that there are three items.
- 02:18 So to get the last thing, we could go, put some space here.
- 02:23 We go, first_names.
- 02:25 And then inside of here, we could put this, minus 1 because
- 02:31 this is going to return 3 minus 1 would give us 2 which is 0, 1, 2, which is Mary.
- 02:37 So if this works, it should print out Mary.
- 02:41 So that's kind of an interesting little hack that you can use to find
- 02:45 the last item in your list.
- 02:46 Because a lot of times, you may have hundreds, even thousands,
- 02:49 of items in your list.
- 02:50 You're not sure exactly how many are in there.
- 02:52 This is a good way to find the last one or the length of them, or whatever.
- 02:56 So, very, very cool.
- 02:58 Like I said, we can update these.
- 02:59 We can add things.
- 03:00 We can remove things.
- 03:01 Let's say we want to change John to Bob.
- 03:04 We just go first_names, put in our index number of 0, which is John.
- 03:09 And just set it equal to whatever we want.
- 03:12 Say Bob, and now if we print this out, we see Bob, Tim and Mary.
- 03:16 So let's say we want to get rid of John completely.
- 03:19 We use the delete command, del and we go first_names and
- 03:23 then pass in our index number of 0.
- 03:26 So we save this and run it, now it's just Tim and Mary.
- 03:29 Finally, we can just add something to the end of our list by just
- 03:34 naming the list first_names, and then doing this .append and
- 03:39 even as we start to type or thing knows already and now we can pass in.
- 03:44 But let's say, let's put Bob at the end.
- 03:47 Now if we save this and run it, we get John, Tim, Mary and Bob.
- 03:51 So those are lists, very easy to work with, very useful.
- 03:55 You'll use the lists for lots of things over and over again.
- 03:58 They're just a very fundamental programming concept.
- 04:01 So in the next video we'll look at lists of lists,
- 04:04 kind of hard to say, lists of lists.
Lesson notes are only available for subscribers.