Hello, everyone. And welcome back to Cyberia on demand. I am your instructor, Joe Perry, and you're watching Intro Toe Python. This is less than eight where we're gonna be doing a deep dive on lists and in this lesson, we're gonna learn two things. The first is we're gonna learn some list methods as we talk about string. Method's gonna do the same thing with lists and we're going to start up
applying slicing toe list. So we're just gonna go over kind of the same things we did with strings,
But this time we're gonna apply them to lists.
So to do that, we're gonna generate our first list, which we're gonna call l one. And that's gonna consist of all the number three being one in 10.
L one, as you can see exists, contains the numbers who wanted to exist.
Let's look at the possible functions we can call against that list.
So have upend clear copy count, extend, index, insert pop, remove reverse and sort those air. All of the built in methods for lists. We're gonna discuss most of them pretty quickly. So first, let's look at
by examining it with
See, we can see here that upend is a method of the built in list. Instance.
We do upended. Then we give it some object argument,
and it depends the object to the end of the list.
Fair enough. Let's see what that looks like practically will do. L one dot upend
It's not my commands here.
01 again. And we can see here that the number 11 has been added to our list.
Fantastic. So I would know how to add things to the list.
What other functions did we have?
l one dot clear. Let's see what that does.
Well, pretty straightforward. It clears the list out.
We're not gonna quite go, you know, item by item through this. But I am going to just demonstrate a few of these methods for your real fast
so we talked about upend the other one that we really want to talk about right away is pop.
Pop is a function that will take the last item off of the list and will print it to your screen will return it to your variable, depending on what you're doing. So if I were to do X equals
you'll see that the list no longer contains the number 10. It is now stored in the variable that X.
There's a really easy way when you use things like pop and upend. What you're actually doing is creating what we call a stack. The idea of a last in first out structure
when you're implementing stacks in Python, you actually will generally be using the penned and pop functions very, very useful tools.
Looking back on our list of options, we have two more that we really want to examine. First is reverse. Reverse is gonna do something pretty similar to what we've seen before.
But what's cool about it
is that it's not actually gonna return anything. That's why I wanted to address this is it was actually happening with. This list is happening inside the list without ever actually returning any code. You're not creating a new list. You are reversing the existing list in place, and then,
as a useful was useful about that, is that it lets us demonstrate the next function that I want to talk about real fast,
The last list function. Sorting lists is a challenge in basically every programming language. It's something that you'll see in a lot of job interviews. The task you'll have pretty often in Python. You can very easily do it
just by running that sort command and real fast. Let's look at sort under helped as well.
You see, it has two arguments, key and reverse, stable sort in place. Basically, what it's saying is, if you have a key, you have some order in which you want some method it mechanism by which you want to start. You can give that as an argument, you know, also reverse sort it.
But we've sorted our list here, and we can see that L one is back to being 123456789 There's some other list methods, and in our lab you'll get to play with those a lot. I definitely recommend you check them out, but for now, now that we understand what the methods are and how to apply them and how to find them and find information about them. We're gonna move on to the second half of this lesson, which is a playing slicing two lists. Now, if you remember,
Waken have, for example, our string
and we could do slicing in that
of reversing the lists or reversing the string with negative one weaken Do X is
You know, X is the entire list, but only every second letter,
all that slicing works exactly the same way with lists. And I actually I actually accidentally said list a couple of times in there. I think
this methodology is interchangeable.
So we can do, For example, we have our l one we could do l one, and we only want the fifth item.
This, by the way, is why I really should stop using l. Because I keep mixing up, Ellen one. But I'm gonna keep doing it because, well, there's no one here to tell me I can't. But here we see the index of five and six makes sense. The fifth number of the sixth number rather is 6012345
We could also do l one
Now the thing that's interesting here and the reason why I wanted to address it why I brought reverse up earlier is that l one by itself, actually is still the same. So l one
with the negative one index step and l one dot reverse
are not actually the same thing reverses sorting that, reversing that list in place. It changes the list itself,
whereas just using the indexes is something that you can do to reverse it without ever actually modifying the list. Additionally, if you wanted to copy out one,
you wouldn't actually do something like l q equals out one. Because then you're using the same list what you would do there and it to demonstrate that we've got l two and one.
You can see that by modifying l two we modified out one because it's the same list. So that's one of the dangers of using lists the way you would copy a listen. First off, re sort no one.
Well, you would. Copulate is actually by using slicing like we just talked about.
all of l one with no step.
And actually, you can get rid of the second Colin there because you're not using a step so all of l one
and then you would just assign that
So now we could do l two dot reversed
and we see that l one stays the same. So when you're trying to copy a list you want to use string lee string slicing or list slicing rather to do that
string slicing and list lacing. The reason why I keep using them interchangeably because they function in essentially the exact same way. So everything that we've done with strings, you'll also get to do with lists in the lab while you're working on those exercises which I highly recommend you do. Ah, that's gonna be all there is for this lesson again. In this lesson, we learned how to apply and use some list methods.
And then we took string slicing, and we turned it into a list slicing.
I hope you enjoyed it. I hope you found it informative. As always, I have been your instructor, Joe Perry, and thank you for watching intruder python on cyber eri on demand