WEBVTT

00:00.740 --> 00:06.710
Often you will want to start with an existing list and make an entirely new list based on the first

00:06.710 --> 00:06.980
one.

00:06.980 --> 00:14.370
So let's explore how copying a list works and examine one situation in which copying a list is useful.

00:14.390 --> 00:21.080
So to copy a list, you can make a slice that includes the entire original list by omitting the first

00:21.080 --> 00:24.290
index and the second index like this.

00:25.410 --> 00:25.930
Right.

00:26.170 --> 00:33.340
So this tells Python to make a slice that starts at the first item and ends with the last item producing

00:33.340 --> 00:35.110
a copy of the entire list.

00:35.230 --> 00:42.550
For example, imagine we have a list of four list of our favorite car makers and want to make a separate

00:42.550 --> 00:47.470
list of car makers that our friend drives.

00:47.470 --> 00:53.710
So this friend likes everything in our list and drives everything on our list so far so we can create

00:53.740 --> 00:56.660
their list by copying ours.

00:56.690 --> 01:03.160
So in order to do that, you will write friend's friend that.

01:05.610 --> 01:07.140
Friend that.

01:08.150 --> 01:10.310
Likes cars.

01:10.310 --> 01:14.030
And here we will use this and let's actually my.

01:15.280 --> 01:17.140
My favorite.

01:19.710 --> 01:21.150
Custom cars.

01:21.150 --> 01:22.380
And here.

01:23.280 --> 01:24.060
Friends.

01:29.940 --> 01:31.200
And here.

01:31.890 --> 01:33.480
Now we will use this.

01:33.510 --> 01:35.310
My favorite cars.

01:35.580 --> 01:36.570
And this.

01:37.760 --> 01:39.050
And after that we will.

01:39.050 --> 01:39.890
Uh, firstly.

01:40.860 --> 01:45.090
Prentiss Brown's favorite cars and after that.

01:45.090 --> 01:50.790
So firstly, of course, this is the friend favorite cars and this is my favorite cars.

01:50.790 --> 01:55.380
And after that we first we will first print the my favorite cars.

01:55.380 --> 01:56.790
So print.

01:58.000 --> 01:59.920
My favorite.

02:02.030 --> 02:04.580
Cars are like this.

02:04.580 --> 02:07.670
And after that we will print our my favourite cars.

02:07.760 --> 02:09.740
And after that we will print.

02:09.770 --> 02:12.140
Of course we need to add new line before it.

02:12.200 --> 02:14.750
So my friends.

02:17.650 --> 02:21.220
Favorite cars are here.

02:21.340 --> 02:22.960
And after that we will print.

02:23.450 --> 02:24.430
Friends.

02:25.000 --> 02:25.900
Friends.

02:25.900 --> 02:26.950
Favorite cars.

02:27.870 --> 02:30.480
And here we will run this now.

02:30.480 --> 02:32.480
And as you can see, this is our output.

02:32.490 --> 02:35.760
So I added this new line here.

02:35.790 --> 02:37.440
Oops, sorry again.

02:41.290 --> 02:42.700
It's not working or.

02:42.700 --> 02:44.320
Yes, it's working now.

02:44.350 --> 02:44.830
Okay.

02:45.930 --> 02:52.140
So here I added this new line character to this to show our output.

02:52.170 --> 02:53.040
New line here.

02:53.040 --> 02:56.010
So I could we added this new line after the.

02:58.400 --> 03:01.310
My favorite cars here, but it's more easy to add here.

03:01.310 --> 03:07.430
Instead of writing some formatting and things in last my favorite cars print function.

03:07.760 --> 03:18.950
So first we make a list of car makers we we like called or favorite car makers called my favorite cars.

03:18.950 --> 03:24.470
Then we make a new list called Friends, Friends, Favorite Cars.

03:24.830 --> 03:34.880
And we make a copy of my favorite cars by asking for a slice of my favorite cars without specifying

03:34.880 --> 03:36.950
an indicator like this here.

03:37.840 --> 03:43.060
And assign the copy of my favorite cars to.

03:43.890 --> 03:45.000
Friend's favorite cars.

03:45.000 --> 03:52.800
So when and when we print each list, we see that they both contain the same cars.

03:52.800 --> 03:57.090
So to prove that we actually have two separate lists, we will add a new.

03:57.830 --> 03:59.380
Uh, we will add a new.

04:02.200 --> 04:09.640
Favor of a new car to each list and show that we that each list keeps track of the appropriate person's

04:09.640 --> 04:10.710
favorite car.

04:10.720 --> 04:15.880
So in order to do that, we will do after my favorite cars here we will do.

04:18.700 --> 04:19.540
My.

04:21.120 --> 04:24.510
My favorite cars that append here.

04:24.540 --> 04:29.550
Let's add new car maker name in this case, Toyota.

04:29.550 --> 04:30.010
Right.

04:30.030 --> 04:31.920
So Toyota.

04:33.500 --> 04:33.620
Of.

04:35.100 --> 04:35.970
Toyota here.

04:35.970 --> 04:42.690
And after that, we will also add friends favorite cars and we will append the.

04:44.230 --> 04:44.990
No, that's sad.

04:49.500 --> 04:50.430
Fiat, right?

04:51.500 --> 04:52.700
And here.

04:53.630 --> 04:59.690
We will not change this print functions and now we will run our code and see what will change here.

04:59.690 --> 05:07.640
And as you can see here, so we copy the original items in my favorite cars to the new list called Friends

05:07.640 --> 05:10.460
Favorite Cars, as we did in the previous example.

05:10.460 --> 05:19.550
And next we add a new favorite new car maker to each list and we add Toyota to my favorite cars and

05:19.550 --> 05:22.940
we add fiat to friends, favorite cars.

05:22.940 --> 05:31.210
And when and we then print the to list to see whether their whether each of these favorite cars in the

05:31.250 --> 05:32.660
is in the appropriate list.

05:32.660 --> 05:36.020
So here the output shows that our.

05:37.140 --> 05:40.710
Toyota is appears in our list of favorite cars.

05:40.710 --> 05:43.320
But Fiat here.

05:43.350 --> 05:45.450
Fiat does not.

05:45.450 --> 05:51.390
So we can see that Fiat now appears in our friends list.

05:51.780 --> 05:53.780
But Toyota doesn't write.

05:53.790 --> 06:01.980
If you had a simple set friend, favorite friends, favorite cars equal to my favorite cars, we will

06:01.980 --> 06:03.330
not produce two separate lists.

06:03.330 --> 06:11.490
For example, here's what happens when you try to copy a list without using a slice.

06:17.560 --> 06:18.940
Without using a slice.

06:19.180 --> 06:22.000
And as you can see here, we both have.

06:22.570 --> 06:30.940
So instead of assigning a copy of my foods to my my favorite cars, to my friend's favorite cars, we

06:30.940 --> 06:34.690
set a friend's favorite cars equal to my favorite cars.

06:34.690 --> 06:41.380
So this syntax actually tells Python to associate the new variable to my favorite cars with a list that

06:41.380 --> 06:45.400
is already associated with my favorite cars.

06:45.400 --> 06:47.830
So now my favorite cars.

06:47.830 --> 06:52.150
It will also appear in friends, favorite cars and so on.

06:52.150 --> 06:59.770
So likewise, the Toyota will appear in both lists, even though or Fiat will appear in both lists,

06:59.770 --> 07:03.280
even though it appears to be added only to my favorite cars.

07:03.280 --> 07:08.110
And as you can see here, Toyota and Fiat appears in the same list.

07:08.110 --> 07:16.270
So the output shows that both lists are the same now, which is not we wanted here and don't worry about

07:16.270 --> 07:17.600
the details in this example.

07:17.600 --> 07:23.360
For now, if you are trying to work with the with a copy of list and you see an expected behavior,

07:23.360 --> 07:30.080
make sure you are copying the list using a slice as we did in this in the first example here.
