WEBVTT

00:00.000 --> 00:03.165
>> Hello everyone and welcome
back to Cybrary On Demand.

00:03.165 --> 00:05.100
I am your instructor
Joe Perry and

00:05.100 --> 00:07.170
you are watching
Intro to Python.

00:07.170 --> 00:08.310
This is Lesson 8,

00:08.310 --> 00:10.820
where we're going to be
doing a deep dive on lists.

00:10.820 --> 00:12.510
In this lesson we're going
to learn two things.

00:12.510 --> 00:14.730
The first is we're going
to learn some list methods

00:14.730 --> 00:16.290
as we talked about
string methods,

00:16.290 --> 00:17.970
going to do the same
thing with lists and then

00:17.970 --> 00:19.800
we're going to start
applying slicing to list.

00:19.800 --> 00:20.985
We're just going to go over

00:20.985 --> 00:22.815
the same things we
did with strings,

00:22.815 --> 00:25.320
but this time we're going
to apply them to lists.

00:25.320 --> 00:27.390
To do that we're going to
generate our first list,

00:27.390 --> 00:28.770
which we're going to
call l1 and that's going

00:28.770 --> 00:30.255
to consist of all
the numbers between

00:30.255 --> 00:35.940
one and 10. l1,

00:35.940 --> 00:37.110
as you can see exists,

00:37.110 --> 00:38.880
contains the numbers
we want it to exist.

00:38.880 --> 00:41.190
Let's look at the
possible functions

00:41.190 --> 00:43.410
we can call against that list.

00:43.410 --> 00:46.520
We have append, clear,
copy, count, extend,

00:46.520 --> 00:49.310
index, insert, pop,
remove, reverse and sort.

00:49.310 --> 00:51.440
Those are all of the
built-in methods for lists

00:51.440 --> 00:53.660
we're going to discuss most
of them pretty quickly.

00:53.660 --> 00:57.285
First let's look at l1.append,

00:57.285 --> 01:03.930
and we can do that by examining
it with this first help.

01:06.680 --> 01:09.710
We can see here that
append is a method of

01:09.710 --> 01:11.960
the built-in list instance we

01:11.960 --> 01:14.480
do append and then we give
it some object argument,

01:14.480 --> 01:17.635
and it appends the object
to the end of the list.

01:17.635 --> 01:20.295
Fair enough, let's see what
that looks like practically.

01:20.295 --> 01:24.450
We'll do l1.append
the number 11.

01:25.610 --> 01:30.910
Mixing up my commands
here, there we go.

01:33.650 --> 01:36.580
L1, again, and we can
see here that the number

01:36.580 --> 01:38.905
11 has been added to our list.

01:38.905 --> 01:41.605
Fantastic. Now we know how
to add things to the list.

01:41.605 --> 01:44.390
What other functions
did we have?

01:44.850 --> 01:48.790
We have clear l1.clear.

01:48.790 --> 01:50.665
Let's see what that does.

01:50.665 --> 01:54.245
Well, pretty straightforward
it clears the list out.

01:54.245 --> 02:01.150
We're not going to quite go
item by item through this,

02:01.150 --> 02:02.575
but I am going to
just demonstrate

02:02.575 --> 02:05.120
a few of these methods
for your real fast.

02:06.350 --> 02:13.785
There's that. We
talked about append,

02:13.785 --> 02:14.980
the other one that we
really want to talk

02:14.980 --> 02:16.390
about right away is pop.

02:16.390 --> 02:18.130
Pop is a function that will take

02:18.130 --> 02:20.530
the last item off of
the list and will

02:20.530 --> 02:22.150
print it to your screen or will

02:22.150 --> 02:23.230
return it to

02:23.230 --> 02:24.585
your variable depending
on what you're doing.

02:24.585 --> 02:30.270
If I were to do x equals
l1.pop and then print

02:30.270 --> 02:33.170
x and print l1

02:33.170 --> 02:34.750
you'll see that
the list no longer

02:34.750 --> 02:36.145
contains the number 10.

02:36.145 --> 02:39.130
It is now stored in
the variable at x.

02:39.130 --> 02:40.810
It's a really easy
way when you use

02:40.810 --> 02:42.700
things like pop and append,

02:42.700 --> 02:43.930
what you're actually doing is

02:43.930 --> 02:45.445
creating what we call a stack.

02:45.445 --> 02:48.055
The idea of a last-in,
first-out structure.

02:48.055 --> 02:50.475
When you're implementing
stacks in Python,

02:50.475 --> 02:52.070
you actually will
generally be using

02:52.070 --> 02:54.485
the append and pop functions.

02:54.485 --> 02:56.970
Very, very useful tools.

02:58.130 --> 03:00.830
Looking back at our
list of options,

03:00.830 --> 03:03.110
we have two more that
really want to examine.

03:03.110 --> 03:04.504
First is reverse.

03:04.504 --> 03:05.990
Reverse is going to
do something pretty

03:05.990 --> 03:07.910
similar to what
we've seen before,

03:07.910 --> 03:10.790
but what's cool about it
is that it's not actually

03:10.790 --> 03:12.010
going to return anything

03:12.010 --> 03:13.445
and that's why I wanted
to address this is.

03:13.445 --> 03:15.590
What's actually happening
with this list is

03:15.590 --> 03:17.975
happening inside the list

03:17.975 --> 03:19.550
without ever actually
returning any code.

03:19.550 --> 03:21.050
You're not creating a new list,

03:21.050 --> 03:24.420
you are reversing the
existing list in place.

03:24.420 --> 03:27.200
Then what's useful about

03:27.200 --> 03:27.920
that is that it lets us

03:27.920 --> 03:29.030
demonstrate the
next function that

03:29.030 --> 03:34.990
I want to talk about real
fast, which is sort.

03:34.990 --> 03:36.675
The last list function.

03:36.675 --> 03:38.640
Sorting lists is a challenge

03:38.640 --> 03:40.110
in basically every
programming language.

03:40.110 --> 03:42.280
It's something that you'll
see in a lot of Java reviews,

03:42.280 --> 03:43.780
is a task you'll
have pretty often.

03:43.780 --> 03:47.140
In Python you can very
easily do it just by

03:47.140 --> 03:48.520
running that sort
command and real

03:48.520 --> 03:51.410
fast let's look at sort
under health as well.

03:55.650 --> 03:58.360
You see it has two arguments,

03:58.360 --> 04:01.690
key and reverse,
stable sort in place.

04:01.690 --> 04:03.640
Basically what it's saying
is if you have the key,

04:03.640 --> 04:07.870
you have some mechanism by
which you want to sort it,

04:07.870 --> 04:09.070
you can give that
as an argument,

04:09.070 --> 04:10.865
you can also reverse sort it.

04:10.865 --> 04:12.670
But we've sorted our
list here and we

04:12.670 --> 04:14.290
can see that l1 is
back to being 1,

04:14.290 --> 04:15.775
2, 3, 4, 5, 6, 7, 8, 9.

04:15.775 --> 04:17.330
There's some other
list methods and

04:17.330 --> 04:19.085
in our lab you'll get to
play with those a lot.

04:19.085 --> 04:20.810
I definitely recommend
you check them out,

04:20.810 --> 04:22.250
but for now that we understand

04:22.250 --> 04:23.750
what the methods are and
how to apply them and

04:23.750 --> 04:25.620
how to find them and find
information about them

04:25.620 --> 04:27.890
we're going to move on to the
second half of this lesson,

04:27.890 --> 04:29.750
which is applying
slicing to lists.

04:29.750 --> 04:32.240
Now if you remember, we
can have, for example,

04:32.240 --> 04:36.960
our string and we can do

04:36.960 --> 04:39.780
slicing in that of

04:39.780 --> 04:42.180
reversing the list or
reversing the string.

04:42.180 --> 04:43.590
With negative 1,

04:43.590 --> 04:47.775
we can do x is two
through five into.

04:47.775 --> 04:49.940
We can do x

04:49.940 --> 04:53.075
is the entire list but
only every second letter.

04:53.075 --> 04:56.660
All that slicing works
exactly the same way with

04:56.660 --> 04:58.280
lists and I actually

04:58.280 --> 05:00.350
accidentally said list a
couple of times in there,

05:00.350 --> 05:04.975
I think because this
methodology is interchangeable.

05:04.975 --> 05:08.090
We can do, for example,
we have our l1,

05:08.090 --> 05:12.630
we could do l1 and we
only want the fifth item.

05:14.450 --> 05:18.125
This by the way is why I
really should stop using L,

05:18.125 --> 05:20.180
because I keep
mixing up L and one,

05:20.180 --> 05:21.710
but I'm going to keep
doing it because,

05:21.710 --> 05:23.680
well, there's no one
here to tell me I can't.

05:23.680 --> 05:26.300
But here we see the
index of five is six,

05:26.300 --> 05:27.650
makes sense the fifth number or

05:27.650 --> 05:29.530
the sixth number rather is 6,

05:29.530 --> 05:33.640
0, 1, 2, 3, 4, 5.

05:33.650 --> 05:40.140
We can also do l1 reversed.

05:40.140 --> 05:41.840
Now, the thing
that's interesting

05:41.840 --> 05:43.220
here and the reason why
I wanted to address it

05:43.220 --> 05:45.260
and why I brought
reverse up earlier is

05:45.260 --> 05:48.005
that l1 by itself actually
is still the same.

05:48.005 --> 05:54.480
So l1 with the
negative 1 index step

05:54.480 --> 05:58.315
and l1.reverse are not
actually the same thing.

05:58.315 --> 06:01.625
Reverse is sorting that or
reversing that list in place,

06:01.625 --> 06:04.670
it changes the list
itself whereas just using

06:04.670 --> 06:06.830
the indexes is something
that you can do

06:06.830 --> 06:09.260
to reverse it without ever
actually modifying the list.

06:09.260 --> 06:11.930
Additionally, if you
wanted to copy l1,

06:11.930 --> 06:14.210
you wouldn't actually do
something like l2 equals

06:14.210 --> 06:16.610
l1 because then you're
using the same list.

06:16.610 --> 06:18.020
What you would do and to

06:18.020 --> 06:21.060
demonstrate that
we've got l2 and l1.

06:25.970 --> 06:28.500
You can see that
by modifying l2,

06:28.500 --> 06:30.890
we modified l1 because
it's the same list,

06:30.890 --> 06:32.840
so that's one of the
dangers of using lists.

06:32.840 --> 06:37.045
The way you would copy a
list and first resort l1.

06:37.045 --> 06:39.230
You would copy list
is actually by

06:39.230 --> 06:41.420
using slicing like we
just talked about.

06:41.420 --> 06:44.900
You would just do all of

06:44.900 --> 06:49.010
l1 with no step and
actually you can get rid of

06:49.010 --> 06:50.300
the second colon there
because you're not

06:50.300 --> 06:51.815
using a step so all of

06:51.815 --> 06:59.355
l1 and then you would
just assign that to l2.

06:59.355 --> 07:02.860
Now we can do l2.reverse

07:03.770 --> 07:06.795
and we see that l1
stays the same.

07:06.795 --> 07:08.270
When you're trying
to copy a list,

07:08.270 --> 07:09.710
you want to use string

07:09.710 --> 07:12.080
slicing or list slicing
rather to do that.

07:12.080 --> 07:13.430
String slicing and list slicing

07:13.430 --> 07:15.515
the reason why I keep using
them interchangeably,

07:15.515 --> 07:18.545
it is they function in
essentially the exact same way.

07:18.545 --> 07:20.090
Everything that we've
done with strings

07:20.090 --> 07:21.650
you'll also get
to do with lists.

07:21.650 --> 07:24.034
In the lab while you're
working on those exercises,

07:24.034 --> 07:25.920
which I highly recommend you do.

07:25.920 --> 07:27.660
That's going to be all
there is for this lesson.

07:27.660 --> 07:29.330
Again, in this lesson we
learned how to apply and

07:29.330 --> 07:31.010
use some list methods
and then we took

07:31.010 --> 07:32.600
string slicing and
we turned it into

07:32.600 --> 07:34.430
list slicing. I hope
you enjoyed it.

07:34.430 --> 07:36.680
I hope you found it
informative as always.

07:36.680 --> 07:38.240
I have been your
instructor Joe Perry,

07:38.240 --> 07:40.040
and thank you for
watching intro to

07:40.040 --> 07:43.080
Python on Cybrary On Demand.

