Transcript from the "Lists" Lesson
>> So for lists, I included a very, very handy list cheat sheet in this chapter where he talks about the use. I show you how to create lists, I show you search methods, search speeds. So searching for an item in a list in Python is actually quite slow because you have to check each.
[00:00:21] Value in it and some common methods. So, let's look at lists and practice. You can declare lists two different ways, one is with these square bracket notation which is how you will commonly see lists being declared. You can also declare them,here and this has come back to bite me.
[00:00:48] You can also declare lists by calling the constructor. I got this type error stir object is not callable. That's because I declared a variable names list. Earlier on in this program and I forgot about it. So I'm going to exit my ripple and restart it. Weird, it looks like start ripple is just broken in my VS code.
[00:01:23] So I'm just going to start it the normal way. Because I have my virtual environment activated here, I can just type Python and it will bring up the right thing. So lists can be declared with square bracket notation. They can also be declared by calling that constructor, you're more likely to see.
[00:01:41] This notation, you'll only really spot this when you're converting between different types. And again, we can verify that type right there. Lists have values that are separated by commas. And in order to close off the list, you'll have to put that closing square bracket. If you forget it, especially in the ripple, it's just gonna do that.
[00:02:11] And that means that Python is waiting for input, it's waiting for you to close that list. So, as soon as you enter that closing square bracket that it expects and you press enter that will that will knock you out of that continuation. And let's take a look at names.
[00:02:30] We'll see here that we have a few values. There are some built in methods that let you act on sequences like this. One that you'll see all the time is called Len. That means give me the length of the sequence. You can also access things in lists by index.
[00:02:53] So python, like most programming languages is zero indexed. So if I wanted the first name in this list of names, I would ask for name zero names one names, too. I have three items in my list. What error am I going to see when I asked for names at index three?
>> Out of bounds or whatever?
>> Yeah, so I'm going to see index error list index out of range. And that's because there is no item at the third position. If you wanted to update an item in a list, and you can update an item in a list because a list is mutable, meaning that a list can be changed, you can do that with the index.
[00:03:45] [SOUND] So now if I look at my list of names, I'll see mark, Max and Jean. You might notice that in python, all of the items in the list don't have to be declared on the same line. So you might notice this more when you are working with long strings or large data types.
[00:04:12] So I can declare a list like this and have the opening bracket and then the data on the repple means it's expecting more input. So I can just separate them. These line by line. You'll notice here that trailing commas on like JSON are allowed. And in fact, in Python, I would say they're encouraged because they really help with diffs and version control.
[00:04:38] So I tend to always include the trailing comma in my in my list definitions. Let's declare some lottery numbers. And let me fix that. Put an equal sign. There we go. So now I have a list of lottery numbers. If I wanted to, sort these temporarily, I could call sorted on them.
[00:05:21] You'll see here though, that my original list has not changed. I could also call sorted with reverse, As an argument and this will take the source and then reverse it. Again though, this list has not changed. If I did want to sort in a place, I would have to call a method on the list.
[00:06:04] Called dot sort. Now if I take a look at my lottery numbers variable, you'll see that it has in fact changed. So the difference here is that the sorted is a built in and calling dot sort. I called that sort on the variable itself. That's what allowed it to be changed in place.And, again, if you were curious about what methods are available and list, you can always call dirt on list and then call help on any one of these by saying help, let's say list dot count and that will print out what the value or what this method does as well as what arguments it expects.
[00:06:54] If you wanted to add items to a list, you can call the append method on it, And pass in the new value and that will impact, change the list. You can also insert items into a list by passing in an index. I'm going to say index zero so inserted at the beginning of the list oops dot insert.
[00:07:36] And then if I take a look at that, remember zero is the position and fifty five is the value that I'm inserting will say that it was inserted at the beginning of the list.
>> Is there an equivalent to append an insert doesn't mutate the list.
>> You could concatenate the list with a different one.
[00:07:58] And that would return a new list. So for example, let's say a list of with the number one and three. This will append create a will create a new list with these values upended. But the original list will not in fact be changed. If you wanted to see if an item was in a list, you would use the in keyword and this will either return true if the item is in the list or false If it's not.