>> Hello everyone and welcome back to Cybrary On Demand.
I am your instructor Joe Perry and
you are watching Intro to Python.
This is Lesson 8,
where we're going to be doing a deep dive on lists.
In this lesson we're going to learn two things.
The first is we're going to learn some list methods
as we talked about string methods,
going to do the same thing with lists and then
we're going to start applying slicing to list.
We're just going to go over
the same things we did with strings,
but this time we're going to apply them to lists.
To do that we're going to generate our first list,
which we're going to call l1 and that's going
to consist of all the numbers between
one and 10. l1,
as you can see exists,
contains the numbers we want it to exist.
Let's look at the possible functions
we can call against that list.
We have append, clear, copy, count, extend,
index, insert, pop, remove, reverse and sort.
Those are all of the built-in methods for lists
we're going to discuss most of them pretty quickly.
First let's look at l1.append,
and we can do that by examining it with this first help.
We can see here that append is a method of
the built-in list instance we
do append and then we give it some object argument,
and it appends the object to the end of the list.
Fair enough, let's see what that looks like practically.
We'll do l1.append the number 11.
Mixing up my commands here, there we go.
L1, again, and we can see here that the number
11 has been added to our list.
Fantastic. Now we know how to add things to the list.
What other functions did we have?
We have clear l1.clear.
Let's see what that does.
Well, pretty straightforward it clears the list out.
We're not going to quite go item by item through this,
but I am going to just demonstrate
a few of these methods for your real fast.
There's that. We talked about append,
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 or will
return it to
your variable depending on what you're doing.
If I were to do x equals l1.pop and then print
x and print l1
you'll see that the list no longer
contains the number 10.
It is now stored in the variable at x.
It's a really easy way when you use
things like pop and append,
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 append and pop functions.
Very, very useful tools.
Looking back at our list of options,
we have two more that really want to examine.
First is reverse.
Reverse is going to do something pretty
similar to what we've seen before,
but what's cool about it is that it's not actually
going to return anything
and that's why I wanted to address this is.
What's 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.
Then what's useful about
that is that it lets us
demonstrate the next function that
I want to talk about real fast, which is sort.
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 Java reviews,
is a 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 health as well.
You see it has two arguments,
key and reverse, stable sort in place.
Basically what it's saying is if you have the key,
you have some mechanism by which you want to sort it,
you can give that as an argument,
you can also reverse sort it.
But we've sorted our list here and we
can see that l1 is back to being 1,
2, 3, 4, 5, 6, 7, 8, 9.
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 that we understand
what the methods are and how to apply them and
how to find them and find information about them
we're going to move on to the second half of this lesson,
which is applying slicing to lists.
Now if you remember, we can have, for example,
our string and we can do
slicing in that of
reversing the list or reversing the string.
With negative 1,
we can do x is two through five into.
We can do x
is the entire list but only every second letter.
All that slicing works exactly the same way with
lists and I actually
accidentally said list a couple of times in there,
I think because this methodology is interchangeable.
We can do, for example, we have our l1,
we could do l1 and we only want the fifth item.
This by the way is why I really should stop using L,
because I keep mixing up L and one,
but I'm going to keep doing it because,
well, there's no one here to tell me I can't.
But here we see the index of five is six,
makes sense the fifth number or
the sixth number rather is 6,
0, 1, 2, 3, 4, 5.
We can also do l1 reversed.
Now, the thing that's interesting
here and the reason why I wanted to address it
and why I brought reverse up earlier is
that l1 by itself actually is still the same.
So l1 with the negative 1 index step
and l1.reverse are not actually the same thing.
Reverse is sorting that or 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 l1,
you wouldn't actually do something like l2 equals
l1 because then you're using the same list.
What you would do and to
demonstrate that we've got l2 and l1.
You can see that by modifying l2,
we modified l1 because it's the same list,
so that's one of the dangers of using lists.
The way you would copy a list and first resort l1.
You would copy list is actually by
using slicing like we just talked about.
You would just do all of
l1 with no step and actually you can get rid of
the second colon there because you're not
using a step so all of
l1 and then you would just assign that to l2.
Now we can do l2.reverse
and we see that l1 stays the same.
When you're trying to copy a list,
you want to use string
slicing or list slicing rather to do that.
String slicing and list slicing
the reason why I keep using them interchangeably,
it is they function in essentially the exact same way.
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.
That's going to 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
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 intro to
Python on Cybrary On Demand.