Check out a free preview of the full Python Fundamentals course

The "Lists" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina introduces how to instantiate lists.


Transcript from the "Lists" Lesson

>> Nina Zakharenko: I feel like we've been laying a good foundation this morning, getting comfortable in Python. Getting comfortable in the understanding that one of Python's core tendencies is to use quotations instead of something like curly braces to delineate blocks of code. So, we got the basics down. We know about numbers, we know about strings.

Now it's time to talk about some advanced data types. So, they're the container objects that let us organize other types of objects into one data structure, a collection, so that we can work with it later.
>> Nina Zakharenko: Very quickly, for most of these data structures, I put a little bit of a cheat sheet at the top, so that when you're coming back to this class, a few days later or a week from now, there's one place to look for all the most important information.

>> Nina Zakharenko: So let's look at these lists in practice.
>> Nina Zakharenko: So we've already seen how to create a little hint about how to create a new list right? So in Python an empty list can be created with two square brackets.
>> Nina Zakharenko: But you can also, because everything is an object in Python, you can also call list on the type list with no parameters, and that'll also give you an empty list, right?

So because this is a built-in method, I didn't have to declare it, right? It's not a variable that I made. If you call your variables list, you're gonna end up having problems down the line. All right, and just to confirm, we can just type, and type will tell us that this [INAUDIBLE] square brackets are lists.

Now if I wanna create a list with some items in it, we're going to use the comma to separate items. So we're all typing along and asking questions. Let's make a list of names for these types that can contain other types you wanna name them in the plural right?

So you know names not name, will give me as a programmer hint that this is probably a collection. So let's put some names in here.
>> Nina Zakharenko: And double checking our type, right? Type of names, it's a list. So in the REPL we can easily just type that variable in and see the results.

>> Nina Zakharenko: We can also past our list of names to the print statement and we can print them out, in case we wanna see what's going on inside of a function we can print out our list. Lists also have a very useful method, but the method isn't on them, right?

We've seen mystring.lower, right? That's a method on the string. For example,
>> Nina Zakharenko: We saw this earlier, right? This is a string method. It acts on the string. But to find out the length of the list, we're gonna use a built-in method called Len, but it's not a method that's on the list.

It's just a built-in method in Python. So we would call the function Len and pass names into it. It's because more than lists can have a length right, a stream can have a length.
>> Nina Zakharenko: Set, dictionary, some of the other types that we're gonna talk about also have a length.

So this is just a globally available function. The most important thing about lists is they retain the order of the items in them. In the next section, we're gonna look at some data types that don't. So this is an important distinction here. In order to access the items in a list, we're gonna need to use an index.

Sometimes multiple indexes are also called indices. Just some preference there. Let's take a look at this.
>> Nina Zakharenko: See if this works.
>> Nina Zakharenko: So if I have a list.
>> Nina Zakharenko: And has some strings in it.
>> Nina Zakharenko: We know that lists retain the order of the items within them. And we access the items in the list by index.

So the index for each item here. Index starts at 0 always in programming.
>> Nina Zakharenko: The index of A is 0, B is 1, C is 2. And if we call len of my_list.
>> Nina Zakharenko: What is it?
>> Student: It's three.
>> Nina Zakharenko: Three, right.
>> Nina Zakharenko: Okay so using the index.

>> Nina Zakharenko: To get the item at the index I put the index in square brackets that follow the list variable. So what would this be? Which value?
>> Nina Zakharenko: I'm gonna remove the equals just for any confusion. Okay, my list of 0 is A. Right. And my list of 2?

>> Student2: C.
>> Nina Zakharenko: C. Okay. Make sense?
>> Nina Zakharenko: So let's try this out. We have our list of names. We know that has a length of three. names[0] is Nina, names[1] is Max, names, the index 2 is Rose. So my list is a blank three, what if I try to access the item at position three?

What will I see?
>> Student4: A [INAUDIBLE] error.
>> Nina Zakharenko: Which error?
>> Student2: Index error.
>> Student3: Out of range.
>> Nina Zakharenko: Right, list index out of range. Makes sense, right? You're trying to access an item that's not there.
>> Nina Zakharenko: To update a particular item in the list we use that same square-bracket notation but we just add assignment to the end of it.

So looking at our list of names. Let's say I wanted to update the middle name to 'Jimmy'. I would put the list, put which position I want to update, then equals, just like we've done with variables, and the new value. So now if I look at my list of names, I'll see that the value there has been replaced

>> Nina Zakharenko: Just a little tip about formatting with Python. When I'm declaring my list and I put my first kinda opening square bracket, optionally here, I can just press Enter. And that gives me that dot, dot, dot in the REPL expecting more input. So I can just do the same thing but enter each name on a new line.

For really long lists or lists that have complicated information. Kind of a little bit easier to read. Also unlike in JSON, you can have a comma here on the very last item and close the list, and that's perfectly valid. In fact you'll see this frequently in production Python just because it helps with difs.

If you add more items to the list, well then this last item doesn't change it already has the comma there. Cool. And the list looks exactly the same. So just a few common gotchas with lists.
>> Nina Zakharenko: If I tried to make a new list and if I forget a comma what do I see?

>> Student4: Syntax error?
>> Nina Zakharenko: Right, invalid syntax. Notice how it's pointing at four here but actually the problem is I forgot a symbol between three and four. Sometimes you'll see a syntax error on the next line or the previous line of where the error actually occurred, so sometimes you have to look around a little bit.

Okay, what if I make a list of names. And. I guess this woulnd't work into REPL, so I'm just gonna quickly open a file. You don't have to open the same file. It's just kind of a quick example. If I had a list of names. And that you'll see in the US code when you when you put in an opening bracket at all put in the closing bracket for you, really helpful.

But let's say I deleted by accident and then I put in some more code. I'm gonna just save this as a Python file. Okay, now I'm gonna run it, and we'll see I got a SyntaxError: invalid syntax. Here you'll see that it's pointing at line X equals 5 but that's perfectly valid code.

The error is actually up above. I forgot my closing bracket. And if you installed your linter you'll also see that the X here is underlined in red. And if you mouse over it, it will show you invalid syntax. You don’t even have to run your code. That's just one extra layer of protection.

>> Nina Zakharenko: Okay, makes sense,
>> Nina Zakharenko: And going back to my REPL here, remember that in this terminal window there’s a drop down here where you can select between your REPL, your Python shell, your other shell. And you might have a few options here. I'm just gonna select the REPL.

Okay. Makes sense, right? You have to be very specific with how you format your list.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now