# 2.9 Lists (Deep-Dive) - IP

Video Activity
Join over 3 million cybersecurity professionals advancing their career
or

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