Check out a free preview of the full Python Fundamentals course

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

Nina reviews the methods learned in the previous sections.

Preview
Close

Transcript from the "List Review" Lesson

[00:00:00]
>> Nina Zakharenko: Going back to our little cheat sheet here, lists, part one. You use lists for storing similar items, generally, in cases where items need to be added or removed. You can create them with square brackets, or by calling the list type. To search for items in the list, you call my_list.index with the item that you're searching for.

[00:00:22]
Or you can ask, is this item in my list, with the in keyword. The search speed is slow, right, for a large list, every item has to be checked. So if the value that you're looking for is all the way at the end, you have to check all of those values.

[00:00:40]
Some common methods on list are calling len, on the list to find out the length, calling append with an item, to add an item. Using insert, with an index and an item to insert in the middle of a list, based on the index. And using pop(), with or without the optional argument, to remove items.

[00:01:03]
Is the order of a list preserved, yes, right, and we know that because we can access the items in the list by index. We'll talk about mutability later, but mutability is, can the contents of this item be changed after it is declared? Is the list mutable, yes, is it possible to sort it in place, yep, we can call my_list.sort() or my_list.reverse(), okay?

[00:01:43]
I know, it's a lot to think about, right, all-new data structure, can contain other data structures. The thing about lists is, they can contain any types, right? They can contain strings, numbers, they can also contain other advanced container types, right? You can easily have a list of lists, how to access that is a little bit out of scope for this class.

[00:02:05]
But if you decide you haven't had enough Python today, and you want some homework after class, it's a fun thing to figure out.
>> Student: But the rule is, in the list, all the members have to be the same.
>> Nina Zakharenko: No, nope.
>> Student: They don't, I thought that was a, okay.

[00:02:18]
>> Nina Zakharenko: They tend to be similar items, just kind of in practice, right, just how you would use them, but that's a very good question. Okay, so someone asked, do items in the list have to be the same type, and the answer is no. So if I assign this wacky list that I made to a variable, what is the value at position 0?

[00:02:44]
>> Class: 1.
>> Nina Zakharenko: 1, and at position 2, Nina, yeah. So totally valid in Python, just generally not something you'd see, more out of convention than possibility, if that makes sense.
>> Student2: And the list knows the type, right? So the True's a Boolean, the None's a None type, cuz it's not a string, cuz there's no quotes around it.

[00:03:08]
>> Nina Zakharenko: Right, that's a complicated question, and kind of out of the scope of this class. But yeah, the list knows the variables that has in them, right, or rather the values of the variables, right?

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